Selenium C#超时异常
我开始编写自动测试,当Selenium(Xpath)无法继续执行元素时,Selenium超时错误出现了问题Selenium C#超时异常,c#,selenium,timeout,C#,Selenium,Timeout,我开始编写自动测试,当Selenium(Xpath)无法继续执行元素时,Selenium超时错误出现了问题 private void CheckLogin(IWebDriver driver) { var driver = new ChromeDriver(); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000)); driver.FindEleme
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);
}
}
所以,当在块if{}中找到元素(文本包含值“code”)时,它工作正常,但当未找到元素(文本不包含值“code”)时,系统发送超时错误,我尝试使用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)
发生OpenQA.Selenium.WebDriverException
HResult=0x80131500
Message=对远程WebDriver服务器的URL的HTTP请求http://localhost:56939/session/ 60秒后超时。
Source=WebDriver
堆栈跟踪:
位于OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest请求)
在OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(commandcommandtoexecute)
在OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(commandcommandtoexecute)
在OpenQA.Selenium.Remote.RemoteWebDriver.Execute(stringdrivercommandtoexecute,Dictionary`2参数)
在OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(字符串机制,字符串值)
位于OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(字符串xpath)
在OpenQA.Selenium.By.c_;u DisplayClasse.b_;c(ISearchContext上下文)中
在OpenQA.Selenium.By.FindElement(ISearchContext)上
位于OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By)
由于添加了隐式等待,因此出现超时错误。
让我们以您的示例为例:
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000));
driver.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']")).Click();
在这种情况下,您“告诉”Selenium等待指定的超时,然后它将搜索您想要找到的元素。如果找不到元素,则会出现超时错误
var driver = new ChromeDriver();
driver.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']")).Click();
在本例中,您告诉Selenium自动获取元素,因此,如果找不到该元素,您将得到一个NoTouchElementException
,没有超时,因为在元素出现之前没有时间等待
如果我们把它扩展到显式等待:
var driver = new ChromeDriver();
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 1000));
wait.Until(d => d.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']"))).Click();
在这种情况下,您告诉Selenium继续搜索该元素,直到找到该元素,但如果未找到元素,则搜索时间不得超过指定为
WebDriverWait
参数的最大超时时间。Displayed()将在C#中抛出错误。你需要正确处理这个问题。参考答案