WebDriverWait不等待我指定的元素

WebDriverWait不等待我指定的元素

问题描述:

我试图让我的代码等待一个元素出现,然后再尝试从该元素获取文本.如果我逐步执行允许元素时间出现的代码,则它按预期方式工作,但是如果我在没有断点的情况下运行它,则等待似乎会被忽略,并引发异常.

I am trying to get my code to wait for an element to appear before trying to get the text from the element. If I step through the code allowing the element time to appear it works as expected, but if I run it without breakpoints the wait appears to be ignored and an exception is raised.

我不明白为什么它被忽略了?

I don't understand why it is being ignored?

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement message = wait.Until(driver => driver.FindElement(By.ClassName("block-ui-message")));
string messageText = message.Text;

作为替代方案,您可以为 ElementIsVisible()引入 WebDriverWait ,并且可以使用以下定位器策略:

As an alternative you can induce WebDriverWait for the ElementIsVisible() and you can use the following Locator Strategy:

string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");


使用 DotNetSeleniumExtras.WaitHelpers nuget :

不是很清楚您使用我需要的特定指令的确切含义.如果您使用的是 SeleniumExtras WaitHelpers ,则可以使用以下解决方案:


Using DotNetSeleniumExtras.WaitHelpers with nuget:

Not that super clear what exactly you meant by specific using directive I need. In-case you are using SeleniumExtras and WaitHelpers you can use the following solution:

string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");