textToBePresentInElement() 不适用于准确的文本
textToBePresentInElement() is not working with exact text
我正在尝试查找并等待 "Total" 之后带有文本 "Total " 和 space 的元素。我使用的定位器是:
//td[./text()='Total ']
我为它实现的等待是:
wait.until(ExpectedConditions.textToBePresentInElement(element,"Total "));
我正在 TimeOutException
。如果我删除文本“Total”之后的 space。它的工作。现在的等待实现是:
wait.until(ExpectedConditions.textToBePresentInElement(element, "Total"));
为什么条件 textToBePresentInElement
不适用于准确的文本?
您可以在source code
中看到答案
public static ExpectedCondition<Boolean> textToBePresentInElement(final WebElement element, final String text) {
//...
String elementText = element.getText();
return elementText.contains(text);
//...
}
该方法使用 element.getText();
获取文本,这意味着返回的文本 as rendered by the browser,即没有尾随 space
The Get Element Text command intends to return an element’s text “as
rendered”...
当您使用文本 "Total "
时,您基本上是在检查预期条件 if
"Total".contains("Total ");
这是 false
,但没有尾随 space 它可以工作。
我正在尝试查找并等待 "Total" 之后带有文本 "Total " 和 space 的元素。我使用的定位器是:
//td[./text()='Total ']
我为它实现的等待是:
wait.until(ExpectedConditions.textToBePresentInElement(element,"Total "));
我正在 TimeOutException
。如果我删除文本“Total”之后的 space。它的工作。现在的等待实现是:
wait.until(ExpectedConditions.textToBePresentInElement(element, "Total"));
为什么条件 textToBePresentInElement
不适用于准确的文本?
您可以在source code
中看到答案public static ExpectedCondition<Boolean> textToBePresentInElement(final WebElement element, final String text) {
//...
String elementText = element.getText();
return elementText.contains(text);
//...
}
该方法使用 element.getText();
获取文本,这意味着返回的文本 as rendered by the browser,即没有尾随 space
The Get Element Text command intends to return an element’s text “as rendered”...
当您使用文本 "Total "
时,您基本上是在检查预期条件 if
"Total".contains("Total ");
这是 false
,但没有尾随 space 它可以工作。