Java - Selenium 等待文本出现在所见即所得中
Java - Selenium wait for text to appear in wysiwyg
我一直在等待任何文本出现在所见即所得中。
这是我的代码
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;
}
});
来自这个 link:
等到文本出现在文本字段中
但它不工作(程序不等待)
<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)
我经常在 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 对正在发生的事情做出响应并可靠地等待。当然,您可以调整等待时间以满足您的特定需求。
如何使用 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;
}
});
我一直在等待任何文本出现在所见即所得中。 这是我的代码
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;
}
});
来自这个 link: 等到文本出现在文本字段中
但它不工作(程序不等待)
<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)
我经常在 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 对正在发生的事情做出响应并可靠地等待。当然,您可以调整等待时间以满足您的特定需求。
如何使用 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;
}
});