Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 关闭新打开的选项卡-Python(Selenium)_Python 3.x_Windows_Selenium_Selenium Webdriver - Fatal编程技术网

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])