Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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 如何在选项卡之间交换?_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 如何在选项卡之间交换?

Python 如何在选项卡之间交换?,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在使用Selenium(与Firefox一起)测试一个web服务,我需要能够在多个选项卡之间切换,但我不知道如何做到这一点。我尝试了以下方法,但不起作用。我做错了什么 from selenium import webdriver driver = webdriver.Firefox() # OPEN UP THE MULTIPLE TABS for i in driver.window_handles: driver.switch_to.window(i) # I HAVE T

我正在使用Selenium(与Firefox一起)测试一个web服务,我需要能够在多个选项卡之间切换,但我不知道如何做到这一点。我尝试了以下方法,但不起作用。我做错了什么

from selenium import webdriver

driver = webdriver.Firefox()

# OPEN UP THE MULTIPLE TABS

for i in driver.window_handles:
    driver.switch_to.window(i) # I HAVE TRIED switch_to_window(i) TOO

我还尝试将每个窗口句柄保存在自己的列表中,并使用它代替窗口句柄,但这也不起作用。

这里,我给出了打开和关闭新选项卡的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Firefox()#Chrome("chromedriver.exe") 
try:
    driver.get("http://www.google.co.in")
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') 
    driver.get("http://www.stackoverflow.com")
    #driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w') 
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) 
    #driver.close()
except Exception as e:
    print e

要打开多个窗口,可以执行
窗口。使用
执行脚本打开

driver = webdriver.Chrome()

driver.get("https://www.google.co.in/search?q=search1")

driver.execute_script("window.open(arguments[0], 'win2')", \
    "https://www.google.co.in/search?q=search2")

driver.execute_script("window.open(arguments[0], 'win3')", \
    "https://www.google.co.in/search?q=search3")

driver.execute_script("window.open(arguments[0], 'win4')", \
    "https://www.google.co.in/search?q=search4")

driver.switch_to_window("win4")

谢谢但我已经知道怎么做了…我只是不知道如何在选项卡之间交换。你可以使用驱动程序。通过标记名称(“body”)查找元素。发送旅行选项卡的键(key.CONTROL+key.TAB)。