2011-11-28 71 views
0

我一直在测试一个涉及多个Ajax调用的应用程序,所以我需要等待条件,以便在发出ajax调用时存在/可见元素。我用这两种方法implicitwaitexplicitwait但没有人似乎是为我工作的一个或其他异常产生如下:implicitwait和explicitwait没有解决与Java的Selenium Webdriver中的问题

1.Unable定位元素

2.Element被禁用,所以可能不能用于使用的操作

隐等待如下:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
WebElement we = driver.findElement(By.name("q")); 

问题: 当我测试此代码,浏览器打开后,它会抛出异常在2秒。

结果:异常产生

显等待

WebDriverWait wait = new WebDriverWait(driver, /*seconds=*/3); 
WebElement element = wait.until(presenceOfElementLocated(By.name("q")); 

问题:

当我测试此代码,浏览器打开后,它会引发在2秒内的异常

结果:产生异常。

也使用visibilityOfElementLocated,但它不适用于我。


有没有人遇到过这个问题,或者有人有这方面的解决方案?

+0

请问您可以在ExplicitWait中增加时间值吗? –

回答

0

我不能说我以前遇到过这个问题,但我也写了我自己的自定义DOM轮询类。这就是我所做的。

private int Timer = 180; 

private bool CheckForElement(WebDriver driver,string byType,string selector) 
{ 
    bool elementFound = false; 

    for (int i = Timer - 1; i > 0; i--) 
    { 
     if (!itemFound) 
     { 
      Thread.Sleep(1000); //sets the loop to check every second this can be done at a much faster or slower rate depending on your preferences 
      if (byType.ToLower() == "id") 
      { 
      try{ 
       WebDriver element = driver.FindElement(By.Id(selector); 
       if(element.Displayed) 
       { 
        elementFound = true; 
       } 
      } 
      catch { 
       //Do Nothing Here as we don't need to handle the exception 
      } 
      } 
      else if (byType.ToLower() == "tagname") 
      { 
      try{ 
       WebDriver element = driver.FindElement(By.TagName(selector); 
       if(element.Displayed) 
       { 
        elementFound = true; 
       } 
      } 
      catch { 
       //Do Nothing Here as we don't need to handle the exception 
      } 
      } 
      else if (byType.ToLower() == "cssselector") 
      { 
      try{ 
       WebDriver element = driver.FindElement(By.cssSelector(selector); 
       if(element.Displayed) 
       { 
        elementFound = true; 
       } 
      } 
      catch { 
       //Do Nothing Here as we don't need to handle the exception 
      } 
      } 
      else if (byType.ToLower() == "classname") 
      { 
      try{ 
       WebDriver element = driver.FindElement(By.ClassName(selector); 
       if(element.Displayed) 
       { 
        elementFound = true; 
       } 
      } 
      catch { 
       //Do Nothing Here as we don't need to handle the exception 
      } 
      } 
     } 
     else 
     { 
      i = 0; //stops the loop when the element is found 
     } 
    } 

    return elementFound ; 
}