Webdriver等待TEXT出现

时间:2016-11-30 13:28:07

标签: java selenium selenium-webdriver webdriver wait

点击下拉列表后,我无法点击列表中的元素?

我创建的方法不起作用。

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{
    WebDriverWait wait = new WebDriverWait(driver, 10);

    wait.until(ExpectedConditions.visibilityOf(element));
    WebElement locator = element; 
    locator.click();

    WebElement textToClick =  driver.findElement(By.linkText(textToAppear));
    wait.until(ExpectedConditions.elementToBeClickable(textToClick));
    textToClick.click();
}

使用thread.sleep似乎有效,但我不想使用此方法,任何人都可以推荐一种方法,在我点击主按钮后等待并点击特定的文本元素?

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{
    WebDriverWait wait = new WebDriverWait(driver, 10);

    wait.until(ExpectedConditions.visibilityOf(element));
    WebElement locator = element; 
    locator.click();

    Thread.sleep(3000);
    driver.findElement(By.linkText(textToAppear)).click();;

}

请注意我需要点击BBQ Sauce,当需要点击BBQ Sauce时thread.sleep()成功

enter image description here

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

使用您自己的FluentWait实现,等待点击后出现文本:

Wait wait = new FluentWait<>(this.driver)
    .withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS)
    .pollingEvery(500, TimeUnit.MILLISECONDS)
    .ignoring(StaleElementReferenceException.class)
    .ignoring(NoSuchElementException.class)
    .ignoring(ElementNotVisibleException.class);

WebElement foo = wait.until(new Function() {
   public WebElement apply(WebDriver driver) {
     return element.getText().length() > 0;
   }
});

答案 1 :(得分:0)

感谢您的帮助,以下方法似乎完成了这个诀窍:

    public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{
    WebDriverWait wait = new WebDriverWait(driver, 10);

    wait.until(ExpectedConditions.visibilityOf(element));
    WebElement locator = element; 
    locator.click();

    wait.until(ExpectedConditions.elementToBeClickable(By.linkText(textToAppear)));
    driver.findElement(By.linkText(textToAppear)).click();
}