简体   繁体   English

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

[英]How to extract the text using Selenium and Java?

getText() , getAttribute("textContent") is not returning correct result. getText()getAttribute("textContent")没有返回正确的结果。

This is the html tag with tag:这是带有以下标签的 html 标签:

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

Code trials:代码试验:

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

or或者

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

Returning Applied Configs: instead of complete Text Applied Configs: TEST, MSA TEST, MSACONFIGURABLE .返回Applied Configs:而不是完整的文本Applied Configs: TEST, MSA TEST, MSACONFIGURABLE

I am not sure what mistake I am doing here.我不确定我在这里犯了什么错误。

To extract the text Applied Configs: TEST, MSA TEST, MSACONFIGURABLE as the element is a dynamic element, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies :要提取文本Applied Configs: TEST, MSA TEST, MSACONFIGURABLE因为元素是动态元素,您必须为visibilityOfElementLocated()引入WebDriverWait ,并且可以使用以下任一Locator Strategies

  • Using xpath and getText() :使用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());
  • Using xpath and getAttribute("innerHTML") :使用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"));

Update更新

As an alternative you can induce WebDriverWait for TextToBePresentInElementLocated() :作为替代方案,您可以为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"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Java使用Selenium从元素中提取文本 - How to extract the text from the element using Selenium through Java 如何使用 Selenium 和 Java 从表中提取文本 5000 - How to extract the text 5000 from the table using Selenium and Java 如何使用 Selenium 和 Java 从 td 节点提取文本 - How to extract text from td node using Selenium and Java 如何使用Selenium和Java从html提取文本2? - How to extract the text 2 from the html using Selenium and Java? 如何使用 Selenium 和 Java 提取 DOM 中存在的文本 - How to extract the text present in DOM using Selenium and Java 如何使用 Selenium 和 Java 从强标签的兄弟文本节点中提取文本 - How to extract the text from the sibling text nodes of the strong tag using Selenium and Java 如何使用 Selenium 和 Java 提取不在其自身标签内的文本节点的文本? - How to extract the text of a text node that is not inside a tag of its own using Selenium and Java? 如何在Java中使用Selenium提取href值 - how to extract the href value using selenium in java 如何在 Java selenium 中使用 JavascriptExecutor 获得 Javascript 的响应或如何提取<script> tag text? - How do I get response of Javascript using JavascriptExecutor in Java selenium or how to extract <script> tag text? 如何使用带有 Java 的 Selenium 从输出控制台或输出文本文件中提取特定字符串 - How to Extract particular String from Output console or output text file using Selenium with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM