How to Verify Tooltip using Selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We can verify the tooltip of an element using Selenium webdriver using the getAttribute method. A tooltip text is the one which gets displayed while we hover on that element.

It disappears once we move the mouse away from the element. A tooltip text generally displays the title attribute value of an element. First, we identify the element then apply the getAttribute method on it. The parameter to be passed to this method is title.

Let us investigate the html code of an element - Tools having a tooltip text.

Here, the tooltip text displayed from Tools menu is Tools - Online Development and Testing Tools which is the value set for the title attribute in the html.

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 TooltipVerfy{
   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 n = driver.findElement(By.linkText("Tools"));

      //obtain title attribute
      String s = n.getAttribute("title");
      //verify tooltip text

      if(s.equals("Tools - Online Development and Testing Tools")) {
         System.out.println("Tooltip text matched");
      }else{
         System.out.println("Tooltip text not matched");
      }
      driver.quit();
   }
}

Output

raja
Published on 06-Apr-2021 12:27:07
Advertisements