Python 3.x 关闭新打开的选项卡-Python(Selenium)
我正在尝试关闭已打开的新选项卡Python 3.x 关闭新打开的选项卡-Python(Selenium),python-3.x,windows,selenium,selenium-webdriver,Python 3.x,Windows,Selenium,Selenium Webdriver,我正在尝试关闭已打开的新选项卡 driver.execute_script("window.open('https://www.google.com/');") 然而,它并没有用我尝试过的方式工作。它们是: ActionChains(driver).key_down(Keys.SHIFT).send_keys('w').key_up(Keys.SHIFT).perform() 这一个确实部分起作用(它关闭驱动程序选项卡,该选项卡不是目标): 我们的目标是关闭Chrome(
driver.execute_script("window.open('https://www.google.com/');")
然而,它并没有用我尝试过的方式工作。它们是:
ActionChains(driver).key_down(Keys.SHIFT).send_keys('w').key_up(Keys.SHIFT).perform()
这一个确实部分起作用(它关闭驱动程序选项卡,该选项卡不是目标):
我们的目标是关闭Chrome(Windows)中新打开的窗口/选项卡。要关闭新打开的选项卡,首先必须使用
driver.switch_to.window(driver.window_handles[1])
现在,您应该使用
driver.close()
然后使用切换回主选项卡
driver.switch_to.window(driver.window_handles[0])
driver.switch_to.window(driver.window_handles[0])