无法获取元素的文本。尝试使用getText()和getAttribute(“ innerHTML”)

时间:2019-05-18 10:54:10

标签: selenium selenium-webdriver webdriver

我试图使用getText()和getAttribute(“ innerHTML”)获取锚链接的文本,但无法获取文本。这是HTML:

<tr _ngcontent-c6="">
    <td _ngcontent-c6="" class="text-center0" style="padding-left:25px">ParentText</td>
</tr>
<tr _ngcontent-c6="">
    <td _ngcontent-c6="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td _ngcontent-c6="" class="text-center4" style="text-align: center; color: rgb(111, 156, 7);">
    <a _ngcontent-c6="">$10,000.00</a>
</td>

我尝试了以下XPath:

.//*[contains(text(),'ParentText')]/../following-sibling::tr/td[2]/a

我尝试过的代码:

WebElement childText = driver.findElement(By.xpath(".//*[contains(text(),'ParentText')]/../following-sibling::tr/td[2]/a"));

WebDriverWait wait = new WebDriverWait(driver,20);

wait.until(ExpectedConditions.elementToBeClickable(childText));

PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println("Currency Selected:" +childText.getText());

还尝试了最后一行与

out.println("Currency Selected:" +childText.getAttribute("innerHTML"));

也尝试了常规打印。

System.out.println("Currency is : " +childText.getText());

System.out.println("Currency is : " +childText.getAttribute("innerHTML"));

我既没有收到任何错误,也没有任何文本输出。 在元素上为isDisplayed()传递一个断言。

    Assert.assertTrue(ChildText.isDisplayed());

1 个答案:

答案 0 :(得分:1)

尝试使用textContent

System.out.println("Currency is : " +childText.getAttribute("textContent"));