1

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());
learningQA
  • 115
  • 1
  • 9

1 Answers1

1

Try with textContent.

System.out.println("Currency is : " +childText.getAttribute("textContent"));
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks for the knowledge. Worked like a charm. Could you suggest a resource where I can learn about these things? – learningQA May 19 '19 at 02:48
  • 1
    You can refer to the [Java doc](https://seleniumhq.github.io/selenium/docs/api/java/overview-summary.html) and I generally try to learn what each method will do when I hit `.`. And will [check always the xpath is correct](https://stackoverflow.com/questions/55870609/is-there-a-way-to-learn-xpath-without-using-firebug-or-xpath-as-firefox-is-not-s/55870909#55870909) in the browser before using it in the script. Also you can verify the outputs in the browser console. Got the idea of `textContent`, when working with console. – supputuri May 19 '19 at 03:34
  • I always the console part before I implement an XPath in a script. – learningQA May 19 '19 at 03:44
  • bro, when I write a script, it executes fine sometimes. But sometimes TimeOutExceptions are occurring even though there aren't any modifications in the script. Have you an issue like this? – learningQA May 19 '19 at 03:47
  • checking your other post. – supputuri May 19 '19 at 17:18