硒-C#-Webdriver-无法找到元素 [英] Selenium - C# - Webdriver - Unable to find element
问题描述
在C#中使用硒我试图打开浏览器,导航到Google并找到文本搜索字段.
Using selenium in C# I am trying to open a browser, navigate to Google and find the text search field.
我尝试以下
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 looks like a copy of this question that has already been answered.
我可以向您展示我的工作,这似乎对我来说很有效:
I can show you what I've done, which seems to work well for me:
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试图以比页面加载速度更快的速度查找元素,这是我的解决方案.希望对您有帮助!
This should wait waitTime amount of time until either the element is found or not. I've run into a lot of issues with dynamic pages not loading the elements I need right away and the WebDriver trying to find the elements faster than the page can load them, and this is my solution to it. Hope it helps!
这篇关于硒-C#-Webdriver-无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!