gpt4 book ai didi

java - 如何使用 Selenium 和 Java 提取文本?

转载 作者:行者123 更新时间:2023-12-01 16:18:09 26 4
gpt4 key购买 nike

getText()getAttribute("textContent") 未返回正确的结果。

这是带有标签的 html 标签:

<span>
class="panel-title text-primary header-font ng-binding" ng-click="titleLink()" style="" xpath="1">Applied Configs: TEST, MSA TEST, MSACONFIGURABLE
</span>

代码试验:

driver.findElement(By.xpath("//*[contains(@name,'Applied Configs')]/div[1]")).getAttribute("textContent"));

driver.findElement(By.xpath("//*[contains(@name,'Applied Configs')]/div[1]")).getText();

返回应用的配置:而不是完整的文本应用的配置:TEST、MSA TEST、MSACONFIGURABLE

我不确定我在这里犯了什么错误。

最佳答案

要提取文本应用的配置:TEST、MSA TEST、MSACONFIGURABLE,因为该元素是动态元素,您必须引入 WebDriverWait对于 visibilityOfElementLocated(),您可以使用以下任一 Locator Strategies :

  • 使用xpathgetText():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')]"))).getText());
  • 使用xpathgetAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')]"))).getAttribute("innerHTML"));
<小时/>

更新

作为替代方案,您可以为 TextToBePresentInElementLocated() 引发WebDriverWait :

new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')][contains(., 'Applied Configs:')]"), ","))
System.out.println(driver.findElement(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')][contains(., 'Applied Configs:')]")).getAttribute("innerHTML"));

关于java - 如何使用 Selenium 和 Java 提取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62349865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com