C# Seleneium WebDriver-获取不同的异常

C# Seleneium WebDriver-获取不同的异常,c#,selenium,webdriver,selenium-grid2,C#,Selenium,Webdriver,Selenium Grid2,我正在使用网格模式运行硒测试。 每当我在页面中找不到元素时,就会出现以下异常 “对远程WebDriver服务器的URL的HTTP请求 60秒后超时。“ 我期望出现一个异常,类似于找不到这样的元素。但是我得到了一个超时时间。任何关于这方面的建议都会很有帮助 代码如下 try { switch (findBy.ToLower()) { case "classname": webElement = driver.FindElement(By.Cl

我正在使用网格模式运行硒测试。 每当我在页面中找不到元素时,就会出现以下异常

“对远程WebDriver服务器的URL的HTTP请求 60秒后超时。“

我期望出现一个异常,类似于找不到这样的元素。但是我得到了一个超时时间。任何关于这方面的建议都会很有帮助

代码如下

try
{
    switch (findBy.ToLower())
    {
        case "classname":
            webElement = driver.FindElement(By.ClassName(findByValue));
            break;
        case "cssselector":
            webElement = driver.FindElement(By.CssSelector(findByValue));
            break;
        case "id":
            webElement = driver.FindElement(By.Id(findByValue));
            break;
        case "linktext":
            webElement = driver.FindElement(By.LinkText(findByValue));
            break;
        case "name":
            webElement = driver.FindElement(By.Name(findByValue));
            break;
        case "partiallinktext":
            webElement = driver.FindElement(By.PartialLinkText(findByValue));
            break;
        case "tagname":
            webElement = driver.FindElement(By.TagName(findByValue));
            break;
        case "xpath":
            webElement = driver.FindElement(By.XPath(findByValue));
            break;
    }
}
catch (Exception e)
{
    return null;
}

非常感谢

Pankaj Katiyar认为超时发生是因为您使用了显式等待,这是正确的。Selenium还附带了一个隐式等待,它表示等待的时间无论以秒计有多长,如果元素仍然没有显示,请继续


您还可以尝试使用“等待直到”预期条件,并将它们添加到所有开关案例语句中。

当您使用显式等待时,会发生超时异常,如果在等待给定的时间限制后仍找不到元素,则会引发超时异常。