<html>
<body><h2 title=”I’m a header”> Leading Space</h2>
</body>
</html>
myElement = driver.find_element_by_tag_name("h2")
text = myElement.text
What do you think the result is ? “Leading Space” or ” Leading Space” ?
Let’s check the official document first :
http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#getText
Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.
Indeed, the getText method will trim the leading spaces before return the result.
I checked it during debug selenium test, please see the screenshot below:
