2016-09-20 108 views
0

所以我開始學習使用Selenium和C#的自動化,問題是我可以導航到我的Facebook,谷歌什麼和所有在Firefox上的作品很好。但是,當使用IE瀏覽器時,它會打開網頁,但會引發錯誤「NoSuchElementException」。我使用相同的代碼,其中一個不工作。 這裏是IE代碼Selenium FindElement適用於Firefox,但不適用於IE

 IWebDriver driver = new InternetExplorerDriver(@"C:\folder"); 
     driver.Navigate().GoToUrl("http://www.google.com"); 
     driver.FindElement(By.Name("q")).SendKeys("Hello World"); 

下面是Firefox的代碼

 IWebDriver driver = new FirefoxDriver(); 
     driver.Navigate().GoToUrl("http://www.google.com"); 
     driver.FindElement(By.Name("q")).SendKeys("Hello World"); 

回答

0

這可能是這兩個瀏覽器的計時問題。你應該嘗試使用WebDriverWait等到元素是如下的交互可見之前使用更穩定的代碼: -

IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Name("q"))); 
element.SendKeys("Hello World"); 
+0

IWAIT不存在,你用什麼包是什麼? –

+0

關注此https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_IWait_1.htm –

相關問題