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

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

以上两个查找元素都可以正常工作,可以获取价值,但无法点击,因为该元素不可见

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'

推荐答案

根据错误 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 代码行将为:

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天全站免登陆