我试图让我的代码在尝试从元素中获取文本之前等待元素出现。如果我逐步执行允许元素时间出现的代码,它会按预期工作,但如果我在没有断点的情况下运行它,等待似乎被忽略并引发异常。
我不明白为什么它被忽略了?
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,并且可以使用以下 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,您可以使用以下解决方案:
string messageText = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.ClassName("block-ui-message"))).GetAttribute("innerHTML");
关于c# - WebDriverWait 没有在等待我指定的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57554298/