Selenium 开关选项卡 [英] Selenium Switch Tabs

查看:43
本文介绍了Selenium 开关选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Firefox 不再支持 Control + T 选项卡,我开始使用driver.execute_script("window.open('URL', 'new_window')")

Since Firefox does not support Control + T anymore for the tab, I started using driver.execute_script("window.open('URL', 'new_window')")

我正在尝试显示我打开的不同选项卡的标题并在它们之间切换.对于下面的示例,我希望输出为 facebook、google 并返回到 facebook.现在输出是 facebook、facebook 和 facebook.

I am trying to display the title of the different tab I open and switch between them. For the example below, I expect the output to be facebook, google and back to facebook. Right now the output is facebook, facebook and facebook.

我从这里尝试了答案,但它也不起作用:使用 selenium webdriver 切换回父标签

I tried the answer from here but it also did not work: Switch back to parent tab using selenium webdriver

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.facebook.com/")
print(driver.title)

driver.execute_script("window.open('http://google.com', 'new_window')")
print(driver.title)

driver.switch_to.window(driver.window_handles[0])
print(driver.title)

更新:我尝试了以下代码,但仍然无法正常工作.

UPDATED: I tried the follow code and it still did not work.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.facebook.com/")
print(driver.title)
window_before = driver.window_handles[0]

driver.execute_script("window.open('http://google.com', 'new_window')")
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
print(driver.title)

推荐答案

关于 Tab/Window 切换/处理的几句话:

A few words about Tab/Window switching/handling:

  • 始终跟踪父窗口句柄,以便稍后根据您的用例在需要时返回.

  • Always keep track of the Parent Window handle so you can traverse back later if required as per your usecase.

始终使用 WebDriverWaitexpected_conditionsnumber_of_windows_to_be)Tabs/Windows 之间切换之前.

Always use WebDriverWait with expected_conditions as number_of_windows_to_be(num_windows) before switching between Tabs/Windows.

始终跟踪子窗口句柄,以便您可以在需要时进行遍历.

Always keep track of the Child Window handles so you can traverse whenever required.

始终使用 WebDriverWaitexpected_conditions 作为 title_contains("partial_page_title") 在提取页面标题.

Always use WebDriverWait with expected_conditions as title_contains("partial_page_title") before extracting the Page Title.

这是你自己的代码,上面提到了一些小的调整:

Here is your own code with some minor tweaks mentioned above:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox(executable_path=r'C:WebDriversgeckodriver.exe')
driver.get("http://www.facebook.com/")
print("Initial Page Title is: %s" %driver.title)
windows_before  = driver.current_window_handle
driver.execute_script("window.open('http://google.com')")
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to.window(new_window)
WebDriverWait(driver, 20).until(EC.title_contains("G"))
print("Page Title after first window switching is: %s" %driver.title)
driver.close()
driver.switch_to.window(windows_before)
WebDriverWait(driver, 20).until(EC.title_contains("F"))
print("Page Title after second window switching is: %s" %driver.title)
driver.quit()

  • 控制台输出:

  • Console Output:

    Initial Page Title is: Facebook – log in or sign up
    Page Title after first window switching is: Google
    Page Title after second window switching is: Facebook – log in or sign up
    

  • 这篇关于Selenium 开关选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆