How to get Tooltip Text in Selenium Webdriver?



We can get the tooltip text in Selenium webdriver with help of the method - getAttribute. The attribute title should be passed as a parameter to this method.

This technique is only applicable if the element has a title attribute.

The tooltip text is the one which gets displayed on hovering the mouse over the element. In the below html code, an element having a tooltip has the attribute title and the value set for title is actually the tooltip text.

The below image shows the menu Coding Ground showing the tooltip text as - Coding Ground - Free Online IDE and Terminal.

Syntax

WebElement m = driver.findElement(By.linkText("Coding Ground")); String s = m.getAttribute("title");

Example

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; public class ToolTipTxt{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");       WebDriver driver = new FirefoxDriver();       //implicit wait       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);       //URL launch       driver.get("https://www.tutorialspoint.com/index.htm");       // identify element       WebElement l=driver.       findElement(By.linkText("Coding Ground"));       // get title attribute value       String t = l.getAttribute("title");       System.out.println("Retrieved tooltip text as :" +t);       driver.quit();    } }

Output

Updated on: 2021-04-03T10:49:31+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements