如何等到某些值出现在 HTML 标签上?

How to wait till some value to be present on HTML tag?

我可以使用 Thread.sleep(); 但我的老板不希望我这样做所以我使用了 explicit wait

webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xPath("")));

但效果不佳,因为 xpath 不作为数据(文本)出现在某个时间。

对于这种情况,应用 Explicit wait,这样标签的 attribute 就会更新一些值。

wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("xpath/tag[contains(@Unit,'Abc')]")));

也可以参考

  • 如果您知道将显示什么文本,那么您可以使用 textToBePresentInElementLocated 方法。让我们看看下面的例子,

HTML#1:

  <input class="google"> Text </input>

等到文本出现如果你知道预期的文本

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//input[@class='google']"), "Text"));
  • 如果该值 (abc) 位于属性中,那么您可以使用以下方法。

HTML#2:

  <input class="google" value="Text" >

等到属性有一些值

WebElement element = driver.findElement(By.xpath("//input[@class='google']"));
wait.until(ExpectedConditions.attributeToBeNotEmpty(element, "value"));

如何读取属性文件?

动态 xPath:

/** Code to read properties file **/

String inputData = readProperties.getProperty("Data").trim();
driver.findElement(By.xpath("//input[@class='"+inputData+"']"));
        wait.until(ExpectedConditions.attributeToBeNotEmpty(element, "value"));