[英]How to extract the text using Selenium and Java?
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();
返回Applied Configs:
而不是完整的文本Applied Configs: TEST, MSA TEST, MSACONFIGURABLE
。
我不確定我在這里犯了什么錯誤。
要提取文本Applied Configs: TEST, MSA TEST, MSACONFIGURABLE因為元素是動態元素,您必須為visibilityOfElementLocated()
引入WebDriverWait ,並且可以使用以下任一Locator Strategies :
使用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());
使用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"));
作為替代方案,您可以為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.