【问题标题】:Webdriver Wait for TEXT to AppearWebdriver 等待 TEXT 出现
【发布时间】:2016-11-30 13:28:07
【问题描述】:

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

我创建的方法不起作用。

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酱,需要点击BBQ酱时thread.sleep()是成功的

感谢您的帮助

【问题讨论】:

  • 能否也添加异常跟踪供我们参考?
  • 使用现有的 ExpectedConditions,例如 - textToBePresentInElement 或 textToBePresentInElementValue。检查 API -- textToBePresentInElement.
  • 请贴出相关的HTML。
  • 感谢您的帮助,但问题仍然存在,@Grasshopper 如果我使用 ** 'wait.until(ExpectedConditions.textToBePresentInElementValue(cyoPage.dropdown_Sauce, "BBQ Sauce")); ' ** 但是单击元素 'cyoPage.dropdown_Sauce' 它会引发“空指针异常”,但是如果我仅使用上面的方法,它将根本不会单击下拉菜单,这反过来又会停止该程序从点击“烧烤酱”
  • 嗨@JeffC 可能更容易通过以下方式访问 html:1。访问:pizzahut.co.uk | 2.输入邮政编码并本地化到任何商店| 3. 点击披萨页面 | 4. 选择 CYO1 并点击按钮添加到购物篮 | 5. 然后您将看到“酱汁下拉按钮”

标签: java selenium selenium-webdriver webdriver wait


【解决方案1】:

使用您自己的 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;
   }
});

【讨论】:

    【解决方案2】:

    感谢您的帮助,以下方法似乎可以解决问题:

        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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多