![](/img/trans.png)
[英]Unable to find the Element on the pop up using Selenium Webdriver with C#
[英]Selenium - C# - Webdriver - Unable to find element
在C#中使用selen我正在嘗試打開瀏覽器,導航到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的元素
這看起來像是已經回答過的這個問題的副本。
我可以告訴你我做了什么,這似乎對我有用:
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試圖找到比頁面加載它們更快的元素,這是我的解決方案。 希望能幫助到你!
您可以嘗試使用旋轉等待
int timeout =0;
while (driver.FindElements(By.id("gbqfq")).Count == 0 && timeout <500){
Thread.sleep(1);
timeout++;
}
IWebElement password = driver.FindElement(By.Id("gbqfq"));
這應該有助於確保元素實際上有時間出現。
還要注意,“gbqfq”id有點氣味。 我可能會嘗試一些比id更有意義的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.