Reading Tool Tip text using getAttribute() method of Selenium

Before explaining how getAttribute() method of Selenium is used to read the tool tip text from a web page, I would like to first explain the below:

What is a Tool Tip ?

Tool Tip is a text that will be displayed as a hint, when the mouse cursor is hovered over any web element. By hovering the mouse cursor over an web element having tool tip, the tool tip text will be displayed to the User.

Example for Tool Tip:

In the below image, the Tool Tip text "Free Selenium tutorials" got displayed on hovering the mouse cursor over the web element text "".

Practical Example:

Now lets read the above shown Tool tip text using Selenium WebDriver's getAttribute() method:

1.Open in Firefox browser and scroll down to find the element having Tool Tip text as shown below:

2. Mouse the move cursor over the "" text web element and observe that the Tool Tip will be displayed as shown below:

3.Right click on the "" text and inspect using FireBug as shown below:

4. Observe that the Tool Tip text of the web element "" is available as a value inside the "title" attribute of <p> html tag as highlighted in the above image. In order to retrieve the tool tip text, we need to read the value of 'title' attribute using getAttribute() method as shown in the below Selenium WebDriver code:

5. On running the above shown code, chrome browser will be launched and the above marked line will read the tool tip text from the web element and print the in the Eclipse IDE Console as shown below:

Hence, we have to use getAttribute() method of WebDriver to read the tool tip text from a web page.

No comments:

Blog Index