我编写了示例代码,但它无效。还观察到2个标签只有1个窗口句柄。如何再次切换到父标签?
driver = webdriver.Firefox()
driver.set_page_load_timeout(60)
driver.implicitly_wait(15)
driver.get("https://www.google.co.in")
oldtab = driver.current_window_handle
print oldtab
print driver.title
body = driver.find_element_by_tag_name("body")
print 'new tab opened'
driver.get("http://gmail.com/")
print driver.title
print 'back to old tab'
driver.switch_to_window(oldtab)
print driver.title
for handle in driver.window_handles:
print "Handle = ",handle
答案 0 :(得分:5)
在将Keys
切换为父标签之前,您需要使用handle
切换标签。
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.set_page_load_timeout(60)
driver.implicitly_wait(15)
# First Tab
driver.get("https://www.google.co.in")
oldtab = driver.current_window_handle
print driver.title
time.sleep(3)
# Second Tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t")
driver.get("http://gmail.com/")
newtab = driver.current_window_handle
print driver.title
time.sleep(3)
# Go back to First Tab
driver.find_element_by_tag_name("body").send_keys(Keys.ALT + Keys.NUMPAD1)
driver.switch_to_window(oldtab)
print driver.title
time.sleep(3)
# Go to Second Tab again
driver.find_element_by_tag_name("body").send_keys(Keys.ALT + Keys.NUMPAD2)
driver.switch_to_window(newtab)
print driver.title
time.sleep(3)
答案 1 :(得分:1)
您可以实现此目的的另一种方法是 - 打开两个浏览器实例driver1
和driver2
并在浏览器实例中打开相应的网址并对其执行操作 -
driver1 = webdriver.Firefox()
driver1.get("https://www.google.co.in")
//perform actions for page https://www.google.co.in
driver2 = webdriver.Firefox()
driver2.get("http://gmail.com/")
//perform actions for page http://gmail.com/
答案 2 :(得分:0)
以下解决方案对我有用。
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
time.sleep(5)
ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.NUMPAD1).key_up(Keys.CONTROL).perform()
答案 3 :(得分:0)
Windows中的另一个完整版本(无代码段)(Firefox)
编辑Dhiraj的代码,使其在Windows中的FF41中工作
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.set_page_load_timeout(60)
driver.implicitly_wait(15)
# First Tab
driver.get("https://www.google.co.in")
oldtab = driver.current_window_handle
print driver.title
time.sleep(3)
# Second Tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t")
driver.get("http://gmail.com/")
newtab = driver.current_window_handle
print driver.title
time.sleep(3)
# Go back to First Tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP)
driver.switch_to_window(oldtab)
print driver.title
time.sleep(3)
# Go to Second Tab again
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP)
driver.switch_to_window(newtab)
print driver.title
time.sleep(3)
driver.close()
打印
Google
Gmail
Google
Gmail
答案 4 :(得分:0)
String str1 = driver.findElement(By.xpath("//*[@class='lft']//div[@class='expColMenu']["+i+"]//div[2]/div[1]/a")).getAttribute("href");
System.out.println(str1);
Thread.sleep(1000);
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(str1);
Thread.sleep(1000);
System.out.println("Title = "+driver.getTitle());
driver.close();
driver.switchTo().window(tabs.get(0));System.out.println();