optimistic_creeper
optimistic_creeper

Reputation: 2799

Why can't we get the text of title tag without getTitle() method

I am curious about tags inside head section especially title tag. Why can't we get text inside that tag like other tags used inside body section? As example:

driver.findElement(By.tagName("title")).getText();

I think, it's a meta tag & there will be only one of it(If I am wrong, correct me). So, selenium provides special method like getTitle()!

Upvotes: 3

Views: 1458

Answers (1)

Florent B.
Florent B.

Reputation: 42518

The Selenium method getText() returns the displayed text. So calling getText() on the title tag will return an empty string since it is not rendered in the page.

To get the textual content, read the 'textContent' attribute/property :

element.getAttribute("textContent")

Spec and default implementation:
https://www.w3.org/TR/webdriver/#get-element-text https://github.com/SeleniumHQ/selenium/blob/master/javascript/atoms/dom.js#L944

Upvotes: 3

Related Questions