【问题标题】:Java - Selenium wait for text to appear in wysiwygJava - Selenium 等待文本出现在所见即所得中
【发布时间】:2017-09-18 07:53:41
【问题描述】:

我一直在等待任何文本出现在所见即所得中。 这是我的代码

           WebDriverWait wait = new WebDriverWait(driver, 70);
        wait.until(new ExpectedCondition<Boolean>(){
            @Override
            public Boolean apply(WebDriver f) {
                WebElement iframeMsg = f.findElement(By.className("cke_wysiwyg_frame"));        
                f.switchTo().frame(iframeMsg);
                WebElement body = f.findElement(By.cssSelector("body"));
                return body.getText().length() != 0;

            }            
        });

从此链接: Wait Till Text Present In Text Field

但它不工作(程序不等待)

<iframe src="" frameborder="0" class="cke_wysiwyg_frame cke_reset" style="width: 100%; height: 100%;" title="Rich Text Editor, editor1" aria-describedby="cke_63" tabindex="0" allowtransparency="true">
	<html dir="ltr" lang="en"><head></head>
	<body contenteditable="true" class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" spellcheck="false"><p><br></p>
	</body>
	</html>


</iframe>

此网站生成内容,大约需要 30 ~ 70 秒,arg 为 45 秒。 我想等待将出现在所见即所得正文中的内容。目前在使用 Thread.sleep(70000)

【问题讨论】:

  • 尝试修剪正文。那里可能有空格。
  • 你在哪一行等待文本?
  • @DebanjanB 在等待。直到我猜
  • @Antoniossss 我仍然不确定 OP 在他使用 f.switchTo().frame(iframeMsg) 的整个 function() 中试图实现什么。使用 EC 会简单得多。

标签: java selenium wysiwyg


【解决方案1】:

我经常在 selenium 中遇到这个问题,我没有尝试过让测试等待它应该等待的时间长度。我没有将整个过程关闭那么长时间,而是实现了一个循环并每秒检查一次:

long start = System.currentTimeMillis(); 
        long secondsPassed = 0;

        while(!condition && secondsPassed < 10)
        {
            secondsPassed = (System.currentTimeMillis()-start)/1000;
            logger.trace("secondsPassed=" + secondsPassed);

            //Code here


            try 
            {
                Thread.sleep(1000);
            } 

            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

这有点小技巧,但我唯一可以等待 selenium 既能响应正在发生的事情又能可靠地等待。当然,您可以调整等待以满足您的特定需求。

【讨论】:

  • 感谢您的宝贵时间。目前我认为这是最好的解决方案。
【解决方案2】:

使用 Selenium 的一些默认功能怎么样:

 /***
 * An expectation for checking WebElement with given locator has text with a value as a part of it
 * @param locator - used to find the element
 * @param pattern - used as expected text matcher pattern
 * @param timeout
 * @return Boolean true when element has text value containing @value
 */
public boolean textMatches(By locator, Pattern pattern, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textMatches(locator, pattern));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking WebElement with given locator has specific text
 * @param locator - used to find the element
 * @param value - used as expected text
 * @param timeout
 * @return Boolean true when element has text value equal to @value
 */
public boolean textToBe(By locator, String value, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBe(locator, value));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the specified element.
 * @param element - the WebElement
 * @param text - to be present in the element
 * @param timeout
 * @return true once the element contains the given text
 */
public boolean textToBePresentInElement(WebElement element, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElement(element, text));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the element that matches the given locator.
 * @param locator - used to find the element
 * @param text - to be present in the element found by the locator
 * @param timeout
 * @return true once the first element located by locator contains the given text
 */
public boolean textToBePresentInElementLocated(By locator, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElementLocated(locator, text));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the specified elements value attribute.
 * @param locator - used to find the element
 * @param text - to be present in the value attribute of the element found by the locator
 * @param timeout
 * @return true once the value attribute of the first element located by locator contains the given text
 */
public boolean textToBePresentInElementValue(By locator, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElementValue(locator, text));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the specified elements value attribute.
 * @param element - the WebElement
 * @param text - to be present in the element's value attribute
 * @param timeout
 * @return true once the element's value attribute contains the given text
 */
public boolean textToBePresentInElementValue(WebElement element, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElementValue(element, text));
    } catch (Exception e) {
        return false;
    }
}

private Wait<WebDriver> getWebDriverFluentWait(int timeout) {
    return new FluentWait<WebDriver>(driver)
            .withTimeout(timeout, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);
}

或者使用其他选项:

Wait wait = new FluentWait(driver)    
.withTimeout(30, SECONDS)    
.pollingEvery(5, SECONDS)   
.ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function() {    
public boolean apply(WebDriver driver) {    
    return driver.findElement(By.id("foo")).getText().length > 0;    
}
});

【讨论】:

  • 所有这些功能都需要一个我没有的预定义文本
  • 我在答案中添加了另一个选项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
相关资源
最近更新 更多