在 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/