我正在尝试查找并等待包含文本“Total”且“Total”后有空格的元素。我使用的定位器是:
//td[./text()='Total ']
我为它实现的等待是:
wait.until(ExpectedConditions.textToBePresentInElement(element,"Total "));
我收到TimeOutException
。如果我删除文本“总计”后面的空格。它正在工作。现在的等待实现是:
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 ,即没有尾随空格
The Get Element Text command intends to return an element’s text “as rendered”...
当您使用文本“Total”
时,您基本上是在检查预期条件
"Total".contains("Total ");
这是false
,但没有尾随空格它可以工作。
关于java - textToBePresentInElement() 不适用于确切的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377235/