Get Tooltip Text in Selenium Python

Profile picture for user arilio666

This article will discuss how to verify the tooltip using Selenium Python.

Before moving on to the code section, we will see the tooltip.

  • A tooltip is a GUI element that displays additional information about an item when the user hovers over or clicks on it. 
  • It is typically a small rectangular box near the item containing text, images, or other content that provides context or clarification about its purpose or function. 
  • Tooltips can be found in various applications, such as web browsers, text editors, and desktop applications, and are often used to provide users with quick help or guidance on how to use the software.
  • This is actually what a tooltip looks like, and it will be displayed once the specific place is hovered upon.
    This is Facebook's login page tooltip message.

Example:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium. web driver.support.UI import WebDriverWait
from selenium. web driver.support import expected_conditions as EC
from selenium. web driver.common.action_chains import ActionChains


driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.facebook.com")
driver.maximize_window()
att = driver.find_element(By.XPATH,"//a[normalize-space()='Groups']").get_attribute('title')
print(att)
if att == "Explore our groups.":
   print('Tooltip verified')
else:
   print('Wrong Tooltip Text')
  • Here we first navigated to the site and then used the get_attribute and passing title as an argument to fetch the selector's tooltip message.
  • And then, using the if-else statement, we can verify the message.
  • Here we can see the print statement of the get attribute.