等待直到文本字段中的文本存在

时间:2013-03-27 10:10:10

标签: java selenium selenium-webdriver

在webdriver中,如何要求webdriver等到文本字段中存在文本。

实际上我有一个kendo文本字段,其值来自数据库,需要一些时间来加载。加载后我可以继续进行。

请帮助解决此问题

5 个答案:

答案 0 :(得分:14)

您可以使用WebDriverWait。来自docs示例:

 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.findElement(...).getText().length() != 0;
            }
        });

答案 1 :(得分:5)

一个工作和使用lambda功能的衬垫。

wait.until((ExpectedCondition<Boolean>) driver -> driver.findElement(By.id("elementId")).getAttribute("value").length() != 0);

答案 2 :(得分:2)

您可以使用一种简单的方法,在该方法中,您需要传递文本将要来的驱动程序对象webelement以及将来的文本。

public static void waitForTextToAppear(WebDriver newDriver, String textToAppear, WebElement element) {
    WebDriverWait wait = new WebDriverWait(newDriver,30);
    wait.until(ExpectedConditions.textToBePresentInElement(element, textToAppear));
}

答案 3 :(得分:1)

使用WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait)和ExpectedCondition(org.openqa.selenium.support.ui.ExpectedConditions)对象

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("element_id"), "The Text"));

答案 4 :(得分:0)

这是我将文本发送到输入的解决方案:

  public void sendKeysToElement(WebDriver driver, WebElement webElement, String text) {
    WebDriverWait wait = new WebDriverWait(driver, Configuration.standardWaitTime);
    try {

        wait.until(ExpectedConditions.and(
                ExpectedConditions.not(ExpectedConditions.attributeToBeNotEmpty(webElement, "value")),
                ExpectedConditions.elementToBeClickable(webElement)));

        webElement.sendKeys(text);

        wait.until(ExpectedConditions.textToBePresentInElementValue(webElement, text));

        activeElementFocusChange(driver);

    } catch (Exception e) {
        Configuration.printStackTraceException(e);
    }
}

WebElement nameInput = driver.findElement(By.id("name"));
sendKeysToElement(driver, nameInput, "some text");