ExpectedConditions.ElementIsVisible returns TimeoutException even when element is present(即使元素存在,ExpectedConditions.ElementIsVisible 也会返回 TimeoutException)
问题描述
我正在使用 Selenium ChromeDriver v2.40,Chrome 版本 67.
I'm using Selenium ChromeDriver v2.40, Chrome version 67.
var driver = Browser.GetChromeDriver();
driver.Navigate().GoToUrl(url);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var abc=driver.FindElement(By.XPath("//*[@id='pdp-size-select']"));
var aaa=wait.Until(d => d.FindElement(By.XPath("//*[@id='pdp-size-select']")));
abc.Click(); // failed because elementisnotvisible
以上两个findelement工作正常,可以取值但是不能点击因为元素不可见
the above two findelement works fine, can get value but cannot click because the element is not visible
所以我继续尝试 ExpectedConditions,但没有运气:
so i go on to try ExpectedConditions, and no luck with this:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='pdp-size-select']")));
以上代码返回:
OpenQA.Selenium.WebDriverTimeoutException: 'Timed out after 10 seconds'
它与 Chrome v67 是否存在向后兼容性问题?
Does it have any backward compatibility issues with Chrome v67?
推荐答案
根据错误 elementisnotvisible 似乎你已经很接近了.当您尝试在元素上调用 Click()
时向前推进,因此将 ExpectedConditions 替换为 ElementIsVisible()
你需要使用 ElementToBeClickable()
如下:
As per the error elementisnotvisible seems you are pretty close. Moving forward as you are trying to invoke Click()
on the element, so instead of ExpectedConditions as ElementIsVisible()
you need to use ElementToBeClickable()
as follows:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='pdp-size-select']"))).Click();
没有对 SeleniumExtras
和 WaitHelpers
的任何引用,代码行将是:
With out any reference to SeleniumExtras
and WaitHelpers
the line of code will be:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='pdp-size-select']"))).Click();
注意:正如您提到的,您使用的是 Chrome v67.x,请确保您使用的是 ChromeDriver v2.40(但不是 ChromeDriver v2.4)
Note: As you mentioned you are using Chrome v67.x ensure that you are using ChromeDriver v2.40 (but not ChromeDriver v2.4)
进一步调试,您似乎拥有 定位器策略经过调整,准确识别 HTML DOM中的两 (2) 个元素一个>.所以你需要构造一个唯一的定位器来识别并点击所需的元素,如下所示:
Debugging further it seems the Locator Strategy you have adapted, identifies exactly two (2) elements within the HTML DOM. So you need to construct a unique locator to identify and click the desired element as follows:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@data-track-action='Product-Page']//following::select[@id='pdp-size-select']"))).Click();
注意:所需元素是 select
元素,如果您希望与 <select>
元素按 最佳实践您需要使用 SelectElement 类来自 OpenQA.Selenium.Support.UI 命名空间.
Note: The desired element is a select
element and if you desire to interect with the <select>
element as per best practices you need to use the SelectElement Class from OpenQA.Selenium.Support.UI Namespace.
这篇关于即使元素存在,ExpectedConditions.ElementIsVisible 也会返回 TimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:即使元素存在,ExpectedConditions.ElementIsVisible 也会返回 TimeoutException
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01