我正在使用Ubuntu14.04中的Selenium和Firefox . 我通过send_keys(Keys.CONTROL't')创建了一个新选项卡,但是在第一个选项卡上继续发出命令,可能是因为我没有正确关注新选项卡 .
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
main_window = driver.current_window_handle
driver.get("https://www.google.com")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.switch_to_window(main_window)
driver.get("http://www.bing.com")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.switch_to_window(main_window)
driver.get("https://github.com/login")
运行此代码时没有错误 .
我转到google.com后创建一个新标签,屏幕变为新标签页上的空白页面 . 之后,我编写了driver.switch_to_window(main_window)来关注新选项卡,以确保新选项卡上会出现下一行代码 . 但是当执行driver.get(“http://www.bing.com ") is executed, screen goes back to the first tab where google.com is present, and changes google.com to bing.com. Same thing for the next code. Another new tab created, but goes back to first tab as driver.get(" https://github.com/login”)并且bing.com成为github.com/login时 .
我以为我专注于当前可见的标签
main_window = driver.current_window_handle; driver.switch_to_window(main_window),
但这并没有奏效 . 如何在3个不同的标签上加载所有3个网页?
规格:
-
Selenium 3.0.2
-
firefox 45.0.1
-
Ubuntu 14.04 amd 64bit
-
python 2.7.6
1 回答
请尝试以下解决方案,如果不是您真正想要的,请告诉我:
打开所有3个窗口后,您可以简单地导航为: