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