我试图让我的代码在尝试从元素中获取文本之前等待元素出现.如果我单步执行允许元素时间出现的代码,它会按预期工作,但如果我在没有断点的情况下运行它,等待似乎会被忽略并引发异常.
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,您可以使用以下解决方案:
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");
这篇关于WebDriverWait 不等待我指定的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!