How to Verify ToolTip text using Selenium WebDriver?

What is ToolTip Text Message?

ToolTip message is the hint to GUI user. Below Screenshot shows ToolTip message.

There are two ways to read and verify tooltip message.

Verify ToolTip  text using Actions Class in Selenium WebDriver

1. Using Actions class

Whenever you focus on element and a tool-tip appears it is displayed inside a “div” which is made visible on mouse focus.

  1. Move to element using actions classActions action = new Actions(driver); action.moveToElement(‘element_to_be_focused’).build().perform();
  1. Now since tooltip displays some text, read that text and find out element from page source HTML where it is mentioned.
  2. Now simply write code to wait for visibility of element having tooltip text.
new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated(‘element_locator’));
  1. Get text from tooltip element using getText()
 Below is sample code  –
package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class VerifyToolTipMessage {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  driver.get("https://jqueryui.com/tooltip/");
  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
  
  new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@id='age']"))).build().perform();
  
  boolean isToolTipDisplayed = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).isDisplayed();
  
  if (isToolTipDisplayed) {
  
   String tooltipText = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).getText();
  System.out.println("Tooltip Text:- " + tooltipText);
  }
  driver.switchTo().defaultContent();
  
  driver.quit();
  
 }

}

2. Using “title” Attribute 

Whenever you will have tooltip then “title” attribute will be there

  package learnAboutActionsClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class VerifyToolTipMessage1 {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");
  String ToolTipText = driver.findElement(By.xpath(".//*[@id='mbtnav']/li[1]/a")).getAttribute("title");
  System.out.println("ToolTip message is: "+ToolTipText);
  driver.quit();
 }

}

Refer below links to learn more about Actions Class in Selenium WebDriver –

Hope this helps !!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories