How to verify the tooltip using the Selenium web driver in Python

Overview

Selenium is an open-source web-based automation tool. In this answer, we learn how to verify the tooltip using the Selenium web driver in Python.

The tooltip is the text that displays on the screen when we hover on the element. It provides more information about that element for users. We can verify the tooltip by getting the value of the title attribute using the get_attribute() method.

Syntax

element.get_attribute("title")

Example

Let's take a look at an example of this.

from selenium import webdriver
import time
#Specify where the Chrome driver present in our PC
PATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe"
#Getting an instance of the web driver
driver = webdriver.Chrome(PATH)
#Providing the website URL
driver.get("https://www.educative.io/")
#Getting the element
ele = driver.find_element("xpath",'//*[@id="__next"]/div[2]/nav/a')
#Checking if the tooltip text matches
if ele.get_attribute("title") == "educative.io":
print("Tooltip is matched")
else:
print("Tooltip text is not matched")

Explanation

  • Line 1: We import webdriver from the selenium package.
  • Line 2: We import time.
  • Line 5: We provide the path where we placed the driver of the web browser. For Chrome, it is chromedriver.exe in the Windows environment.
  • Line 8: We get the instance of the webdriver.
  • Line 11: We provide the URL to the driver.get() method to open it.
  • Line 14: We get the element that has a tooltip using the find_element() method.
  • Lines 17–20: We get the tooltip text of that element using the get_attribute() method, and compare it with a known value. We print the output according to the result of the comparison.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved