learningQA
learningQA

Reputation: 125

Cannot get the text of an element. Tried getText() and getAttribute("innerHTML")

I was trying to get the text of an anchor link using getText() and getAttribute("innerHTML"), but couldn't get the text. Here is the 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>

I've tried the following XPath:

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

The code I've tried:

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());

also tried the last line with

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

Also tried the usual print.

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

and

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

I'm neither getting any errors nor any text output. An assertion is passed for isDisplayed() on the element.

    Assert.assertTrue(ChildText.isDisplayed());

Upvotes: 1

Views: 254

Answers (1)

supputuri
supputuri

Reputation: 14135

Try with textContent.

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

Upvotes: 1

Related Questions