c# - Selenium - C# - Webdriver - 无法找到元素

标签 c# selenium selenium-webdriver webdriver

在 C# 中使用 selenium 我正在尝试打开浏览器,导航到 Google 并找到文本搜索字段。

我试试下面的

IWebDriver driver = new InternetExplorerDriver(@"C:\");

driver.Navigate().GoToUrl("www.google.com");

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

IWebElement password = driver.FindElement(By.Id("gbqfq"));

但出现以下错误-

无法找到 id == gbqfq 的元素

最佳答案

这看起来像是 this question 的副本已经回答了。

我可以向您展示我所做的,这似乎对我很有效:

public static IWebElement WaitForElementToAppear(IWebDriver driver, int waitTime, By waitingElement)
{
        IWebElement wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTime)).Until(ExpectedConditions.ElementExists(waitingElement));
        return wait;
}

这应该等待 waitTime 时间量,直到找到或找不到元素。我遇到了很多问题,动态页面没有立即加载我需要的元素,WebDriver 试图比页面加载元素更快地找到元素,这就是我的解决方案。希望对您有所帮助!

关于c# - Selenium - C# - Webdriver - 无法找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23084078/

相关文章:

html - 使用 Ruby 和 Selenium Webdriver find_elements 根据条件选择并单击特定链接

c# - 在 VSTO PowerPoint 中创建 map 图表

java - Selenium 将文本框视为隐藏,即使我可以在浏览器中看到它

python - 如何使用 selenium python 通过 href 值查找元素?

javascript - 如何使用 Selenium Java WebDriver 捕获 java 脚本错误

java - Selenium Webdriver 远程设置

c# - 谓词和 OrderBy , Func

C# 错误 : An object reference is required for the non-static field, 方法或属性

c# - ClosedXML - 创建多个数据透视表

javascript - 瓦提尔 "Element is no longer attached to the DOM"错误