即使存在元素,ExpectedConditions.ElementIsVisible 也会返回 TimeoutException [英] ExpectedConditions.ElementIsVisible returns TimeoutException even when element is present

查看:32
本文介绍了即使存在元素,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();

没有任何对 SeleniumExtrasWaitHelpers 的引用,代码行将是:

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.所以你需要构造一个唯一的定位器来识别并点击所需的元素,如下所示:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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