In this tutorial, we will discuss the ToolTip and how to automate the ToolTip in Selenium.
What is ToolTip?
A ToolTip is a small, informational popup that appears when a user hovers over an element such as a button, image, or link. It provides additional context or information about the element. Tooltips are often implemented using the title attribute in HTML or through JavaScript and CSS for custom tooltips.

Tool Tips are implemented in different ways–
- The basic implementation is based on HTML’s “title” attribute. getAttribute(title) gets the value of the tooltip.
- Another tool tip implementation is using Action Class.
This tutorial explains the steps to automate the Custome ToolTip where no title attribute is present.
Let’s understand how one can automate the process of verifying tooltip in Selenium.
To test tooltips in Selenium, you generally follow these steps:
1. Locate the Element with Tooltip: Identify the element that contains the tooltip.
2. Hover Over the Element: Use Actions class to simulate mouse hover event.
3. Capture the Tooltip Text: Retrieve the text from the tooltip element.
4. Validate the Text: Assert that the tooltip text matches the expected value.
Implementation Steps
Locate the Tooltip Element
Identify the element that contains the tooltip. Here, the element has been found using By.id().
WebElement elementWithTooltip = driver.findElement(By.id("toolTipButton"));

Hover Over the Element
Identify the element that contains the tooltip using Actions class to hover over the element.
Actions actions = new Actions(driver);
actions.moveToElement(elementWithTooltip).perform();
Retrieve Tooltip Text
Capturing the tooltip text typically found in the title attribute. But in this case, there is no title in page source, so I have created an xpath using contains(text()) to find the tooltip text.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement tooltip = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'You hovered over the Button')]")));
Validate the Text
Comparing the captured text with the expected tooltip text.
if (expectedTooltipText.equals(actualTooltipText)) {
System.out.println("Tooltip text is correct!");
} else {
System.out.println("Tooltip text is incorrect!");
}
The complete program is shown below:
package com.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class TooltipTest_Demo {
static WebDriver driver;
static String expectedTooltipText = "You hovered over the Button";
static String actualTooltipText;
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://demoqa.com/tool-tips");
// Locate the element with the tooltip
WebElement elementWithTooltip = driver.findElement(By.id("toolTipButton"));
// Perform hover action using Actions class
Actions actions = new Actions(driver);
actions.moveToElement(elementWithTooltip).perform();
// Wait for the tooltip to be visible
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement tooltip = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'You hovered over the Button')]")));
// Check the tooltip text
actualTooltipText = tooltip.getText();
System.out.println("Actual Tooltip Text: " + actualTooltipText);
if (expectedTooltipText.equals(actualTooltipText)) {
System.out.println("Tooltip text is correct!");
} else {
System.out.println("Tooltip text is incorrect!");
}
driver.quit();
}
}
The output of the above program is

We are using WebDriverWait to wait for the hover text to appear.
Summary
- Custom ToolTip can be automated using Action class.
- moveToElement(element) of Actions class is used to mouse hover an element.
- Build() method of Actions class builds the sequence of user actions into an Action object.
- Perform() of Action class executes all the sequence of user actions at once.





