Selenium Interview Question 5 – How To Retrieve Title of a Webpage Without Using getTitle() Method

Verifying page title is basic step before proceeding for further validation. Generally we use getTitle() method which gives title of currently loaded page in browser. 

There is also an another way of retrieving title of a web page. Title of webpage is stored in “title” tag under “head” tag. We can locate “title” web element and use getAttribute(“textContent”) on it. It will give the same result. 

We can not use getText() method on title web element as it is not rendered on web page i.e. browser visible area. ‘getText()’ works well on web element defined under body tag. If you use getText() at title here, you will get empty string.

We can retrieve title using JavascriptExecutor as well. 

Output:

Title of webpage by getTitle() method: Google
Title of webpage by webelement title :Google
Title of webpage by Javascript :Google

Like, comment and share it to reach maximum. Thanks. 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: