[英]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()
:使用xpath和
getText()
:
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")
:使用xpath和
getAttribute("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"));
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.