I am trying to get the text of an element using Selenium getText() method and using innerText property of an element and compare the output.
HTML code:
<div>
<h1> DEF <p> Demo Shot <span> Taken by me </span></p> </h1>
</div>
Selenium code:
System.out.println(driver.findElement(By.xpath("//h1")).getText());
**Output:**
DEF
Demo Shot Taken by me
System.out.println(driver.findElement(By.xpath("//h1")).getAttribute("innerText"));
**Output:**
DEF
Demo Shot Taken by me
As per the Selenium doc:
getText() method gets the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.
So, I expect both the outputs should be same. But in this case, the output of getText() and getAttribute("innerText") is completely different. Any reason ?
getText()
that you provide is taken from javascript selenium bindings. Not from Java. Java api does not mention innerText in that method description. It just states that the text is provided "as rendered" selenium.dev/selenium/docs/api/java/org/openqa/selenium/…