c# - WebDriverWait 没有在等待我指定的元素

标签 c# selenium selenium-chromedriver webdriverwait

我试图让我的代码在尝试从元素中获取文本之前等待元素出现。如果我逐步执行允许元素时间出现的代码,它会按预期工作,但如果我在没有断点的情况下运行它,等待似乎被忽略并引发异常。

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

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.WaitHelpersnuget 一起使用:

不是很清楚我需要的特定使用指令到底是什么意思。如果您正在使用 SeleniumExtrasWaitHelpers,您可以使用以下解决方案:

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/

相关文章:

javascript - 在 Chrome 中模拟横向 iPad 方向

java - 从下拉列表中随机选择?

c# - 在 Visual Studio C# 中使用 beautifulSoup 和 request 等 python 模块

c# - 编译器在引用程序集时遇到麻烦

python - Windows 7下Selenium无法启动FirefoxDriver : Can't load profile

linux - "Syntax error: Unterminated quoted string"和 "org.apache.commons.exec.ExecuteException: Process exited with an error: 2 (Exit value: 2)"

python - selenium.common.exceptions.WebDriverException : Message: 'library' executable may have wrong permissions for ChromeDriver

c# - ASP.Net Core docker从服务容器访问服务容器抛出ssl证书错误

c# - 如何在 Xunit 中创建基测试类来测试接口(interface)实现?

c# - Selenium click 方法正在执行然后超时