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.
<div> <h1> DEF <p> Demo Shot <span> Taken by me </span></p> </h1> </div>
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 ?