1
我開始自動測試寫入,並與硒超時錯誤,故障時硒(Xpath的)不能繼續進行元件硒C#超時異常
private void CheckLogin(IWebDriver driver)
{
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000));
driver.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']")).Click();
var element = driver.FindElement(By.XPath(".//*/span[contains(text(),'code')]"));
if (element != null && dealer.Displayed)
{
System.Diagnostics.Debug.WriteLine("Element is shown");
}
else
{
System.Diagnostics.Debug.WriteLine("Element is not shown");
driver.FindElement(By.XPath(".//*[@id='s2id_autogen5']/a")).Click();
driver.FindElement(By.XPath(".//*[@id='body']/a")).Click();
driver.FindElement(By.XPath(".//*[@id='s2id_autogen6_search']")).SendKeys(ENTER);
}
}
所以,當在塊元件如果{}具有被發現(文本包含值「代碼」),它工作正常,但是當它沒有被發現(文本不包含值「代碼」),系統發送超時錯誤,我試圖使用try/catch結構,但它沒有幫助。 ChromeDriver()和FirefoxDriver()的問題相同。
例外:
OpenQA.Selenium.WebDriverException occurred
HResult=0x80131500
Message=The HTTP request to the remote WebDriver server for URL http://localhost:56939/session/ timed out after 60 seconds.
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
如果找不到元素,則.Displayed()將在C#中引發錯誤。你需要正確處理。參考答案[這裏](http://stackoverflow.com/questions/27516545/how-to-check-if-element-exists-in-c-sharp-selenium-drivers) – Sudeepthi