2017-10-10 142 views
1

我想实现自定义等待方法,应该等到加载弹出窗口可见。Customwait - 检查元素是可见/消失与硒webdriver(元素是在DOM中,但不可见)

此加载弹出框有它自己的id =“wait”。我用这个自定义expectedConditions(从#1),以检查它:就当装载仍清晰可见,我不知道为什么

public static ExpectedCondition<Boolean> absenceOfElementLocated(
      final WebElement element) { 
     return new ExpectedCondition<Boolean>() { 
      @Override 
      public Boolean apply(WebDriver driver) { 
       try { 
        element.isDisplayed(); 
        return false; 
       } catch (NoSuchElementException e) { 
        return true; 
       } catch (StaleElementReferenceException e) { 
        return true; 
       } 
      } 

      @Override 
      public String toString() { 
       return "element to not being present: " + element.getText(); 
      } 
     }; 
    } 

我的脚本通。

谢谢!

+0

您可以使用WebDriverWait暂停执行,直到预期条件成立。 ex: WebDriverWait wait = new WebDriverWait(driver,30000); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“yourPopUpId”))); –

+0

谢谢,我试过了,但没有奏效。驱动程序无法识别弹出窗口的更改。 – brobee

+0

尝试使用JavascriptExecutor并执行操作 – iamsankalp89

回答

0

使用ExpectedConditions#invisibilityOfElementLocated(By locator)

您还可以使用否定 - >ExpectedConditions#not(ExpectedCondition condition)

一个基本的例子:
到这个页面:Primefaces - dropdown

有此页面上Submit按钮,如果你点击此按钮,然后Selected消息将出现在屏幕上,然后此消息将在几秒钟后消失。

因此,我们将在我们的代码等待下列事件:

  • 按钮出现,是clickeable(不能点击,直到它不是clickeable)
  • 出现的消息,可见
  • 信息消失的是不可见的

WebDriver driver = new ChromeDriver(); 

try { 
    driver.get("https://www.primefaces.org/showcase/ui/ajax/dropdown.xhtml"); 

    final By buttonSubmit = By.xpath("//button[ *[ text() = 'Submit' ]]"); 
    final By message = By.xpath("//span[ text() = 'Selected' ]"); 

    WebDriverWait wait = new WebDriverWait(driver, 50); 

    long time = System.currentTimeMillis(); 

    wait.until(ExpectedConditions.elementToBeClickable(buttonSubmit)).click(); 
    System.out.println(String.format("Button clicked after %d miliseconds", System.currentTimeMillis() - time)); 

    wait.until(ExpectedConditions.visibilityOfElementLocated(message)); 
    System.out.println(String.format("The message appeared after %d miliseconds", System.currentTimeMillis() - time)); 

    // wait.until(ExpectedConditions.invisibilityOfElementLocated(message)); 
    wait.until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(message))); 
    System.out.println(String.format("The message dissapeared after %d miliseconds", System.currentTimeMillis() - time)); 

} finally { 
    driver.quit(); 
} 

和一个结果是:

Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 15827 
..... 
..... 
..... 
Button clicked after 153 miliseconds 
The message appeared after 791 miliseconds 
The message dissapeared after 6924 miliseconds 
+0

感谢您的代码。我尝试了所有预期的条件,但正如我之前提到的,驱动程序无法识别弹出状态的差异。我用Id。你认为,我应该用xpath来代替吗?我知道它不应该修改任何东西,我只是猜测... – brobee

0

这很难说,为什么你的代码是没有更多的代码工作。您的自定义等待中确实有一些逻辑错误,但您不需要自定义等待,因为ExpectedConditions已经具有可见性和隐形覆盖。

使用此等待弹出出现

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("wait"))); 

使用此等待弹出消失

new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.id("wait"))); 

有些时候,一个弹出窗口只是为动态加载内容的容器。在这些情况下,您可能会等待弹出窗口的框架出现,但框架的内容尚未完全加载,因此如果您尝试与它们进行交互,将会出现错误。在这些情况下,您需要等待容器内的元素可见。

对话框关闭时也是如此。我曾经有过等待对话框容器关闭的经验,但是灰色叠加层仍然阻止点击等。在这些情况下,我必须等待叠加层变得不可见。

我建议你花一些时间熟悉ExpectedConditions的可用方法,并且不必为自己已经存在的东西编写自定义等待,也不需要进行调试/测试。

+0

你好杰夫,谢谢你的回复。主要的问题,所有提到的方法不起作用,因此我尝试了自定义的等待方法。 ExpectedConditions非常简单,但在这种情况下不是。我猜,(只是假设)是主要问题,这个等待元素总是在dom中,每次,每一页,我都可以在源代码中找到它,只是可见性正在改变,webdriver无法识别差异。我尝试了其他网站的这些方法,并且像魅力一样工作,但在这种情况下,我无法使用它。需要一些技巧来解决这个问题。用一些Javascript? – brobee

+0

该元素可能始终位于DOM中,但这并不意味着此方法不起作用。硒可以区分现在和可见/不可见。请分享该网站或示例网站,以便我可以看到。 – JeffC

+0

我不能分享它,但我会检查一些例子。 – brobee