</>

Technology

Selenium

Difficulty

Beginner

Interview Question

How to verify tooltip text using Selenium?

Answer

WebElements expose tooltip text via the title HTML attribute. Use getAttribute("title") to read and verify it.

Basic tooltip verification:

Java
WebElement element = driver.findElement(By.id("helpIcon"));
String toolTipText = element.getAttribute("title");
System.out.println("Tooltip: " + toolTipText);
Assert.assertEquals(toolTipText, "Click here for help");

Full test example:

Java
@Test
public void verifyTooltip() {
    driver.get("https://automateqa.online");
    WebElement infoIcon = driver.findElement(By.cssSelector(".info-icon"));
    String tooltip = infoIcon.getAttribute("title");
    Assert.assertFalse(tooltip.isEmpty(), "Tooltip should not be empty");
    Assert.assertEquals(tooltip, "Expected Tooltip Text");
}

For CSS tooltips (shown via ::after pseudo-element or JS): Some modern tooltips are rendered via JavaScript or CSS pseudo-elements — not in the title attribute. In that case, hover over the element and then read the tooltip element''s text:

Java
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement tooltip = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.className("tooltip-text"))
);
Assert.assertEquals(tooltip.getText(), "Expected Tooltip");

Key summary:

  • getAttribute("title") → for native HTML title tooltips
  • Hover + read element text → for JS/CSS-rendered tooltips

Follow AutomateQA

Related Topics