简体   繁体   English

如何使用预期条件 new_window_is_opened 切换选项卡并使用 Selenium 和 Python 验证页面标题

[英]How to switch tabs using the expected conditions new_window_is_opened and verify the page title using Selenium and Python

I'm writing a test which goes through multiple pages and then verifies the title is being displayed.我正在编写一个测试,它会遍历多个页面,然后验证标题是否正在显示。 Currently my test is at the point where i can click a button and it opens a report.目前我的测试是在我可以点击一个按钮并打开一个报告的地方。 The problem is the report opens up in a new tab so i need my test to move tabs and verify the title in the new tab.问题是报告在新选项卡中打开,所以我需要我的测试来移动选项卡并验证新选项卡中的标题。

The title that needs to be verified is 'valuation'.需要验证的标题是“估值”。 I've done an assert to confirm the title is the same as i expect我做了一个断言来确认标题和我预期的一样

I've written the code below in python.我在 python 中编写了以下代码。 Its currently failing at wait on the 2nd line它目前在第二行等待失败

current = self.driver.current_window_handle
wait(self.driver, 10).until(EC.new_window_is_opened(self.driver.window_handles))
self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])
title = self.driver.title
self.assertTrue("Valuation" == self.driver.title)

I'm opening a new tab with the following lines of code:我正在使用以下代码行打开一个新选项卡:

element = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']") 
driver.execute_script("arguments[0].click();", element)

new_window_is_opened(current_handles) new_window_is_opened(current_handles)

new_window_is_opened(current_handles) is the expectation that a new window will be opened and have the number of windows handles increase. new_window_is_opened(current_handles)是期望打开一个新的 window 并增加 windows 句柄的数量。


Demonstration示范

The following example opens the url http://www.google.co.in first and then opens the url https://www.yahoo.com in the adjacent tab: The following example opens the url http://www.google.co.in first and then opens the url https://www.yahoo.com in the adjacent tab:

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

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("http://www.google.co.in")
windows_before  = driver.window_handles
driver.execute_script("window.open('https://www.yahoo.com')")
WebDriverWait(driver, 20).until(EC.new_window_is_opened(windows_before))
driver.switch_to.window([x for x in driver.window_handles if x not in windows_before][0])

Note : The argument passed to new_window_is_opened(current_handles) is a list.注意:传递给new_window_is_opened(current_handles)的参数是一个列表。 In order to create a list we need to use windows_before = driver.window_handles为了创建一个列表,我们需要使用windows_before = driver.window_handles


This usecase这个用例

In your code block, it is expected that when you:在您的代码块中,预计当您:

current = self.driver.current_window_handle

There exists only one window handle.仅存在一个 window 手柄。 So moving forward, the line of code:所以继续前进,代码行:

wait(self.driver, 10).until(EC.new_window_is_opened(self.driver.window_handles))

would wait for a new window to be opened and have the number of windows handles increases and that would happen only after an action is performed which opens a new window, which seems to be missing from your code block.将等待打开新的 window 并增加 windows 句柄的数量,并且仅在执行打开新 window 的操作后才会发生,这似乎从您的代码块中丢失。


Solution解决方案

Insert the line of code which initiates opening of a new :插入启动新打开的代码行:

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

windows_before  = driver.window_handles
element = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']")
driver.execute_script("arguments[0].click();", element)
WebDriverWait(self.driver, 10).until(EC.new_window_is_opened(windows_before))
driver.switch_to.window([x for x in driver.window_handles if x not in windows_before][0])
assertTrue("Valuation" == driver.title)

Note : As per the lines of code you have updated within your comments you are not using Python class , so you shouldn't use the keyword self .注意:根据您在评论中更新的代码行,您没有使用Python class ,因此您不应使用关键字self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中使用Selenium在由不同WebDriver打开的不同Chrome浏览器窗口之间切换? - How to switch between different chrome browser window opened by different WebDriver using selenium in Python? 如何使用Selenium和Python切换窗口句柄 - How to switch window handles using Selenium and Python 如何使用 Python、JavaScript 或 Robot Framework 和 Selenium 使用部分标题文本导航到新的浏览器窗口 - How to navigate to new browser window using partial title text using either Python, or JavaScript or Robot Framework and Selenium 无法使用 python selenium webdriver 切换到新页面? - can not switch to a new page using python selenium webdriver? 如何在 Selenium for Python 中切换到新窗口? - How to switch to new window in Selenium for Python? 如何使用Python启动带有多个选项卡的新Firefox窗口 - How to launch new Firefox window with multiple tabs using Python 如何在selenium python中获取新打开的标签的链接和页面信息 - How to get link and page information of new opened tab in selenium python 在python +硒中使用Expected_conditions时出现错误 - Getting error in using expected_conditions in python + selenium 如何使用 Python Selenium 验证禁用的按钮 - How to verify a disabled button using Python Selenium 如何使用python的Selenium WebDriver在浏览器上打开一个新的window? - How to open a new window on a browser using Selenium WebDriver for python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM