How To Handle Tooltip in Selenium Webdriver

Hello Friends, In this post, we will learn How to Handle Tooltip  in Selenium Webdriver using different methods.

What is Tooltip?

Tooltip is a small text popup which gives the information of a web element which appears when we mouse hover on the web element like button, image, link etc, if the tooltip assigned to that web element.

It helps user to understand the application.

Now we will see how to handle this tooltip using selenium webdriver.

 

 

How to handle tooltip in Selenium

Steps to handle Tooltip in Selenium using Actions Class

  1. For above image, open this link – https://www.w3schools.com/css/css_tooltip.asp
  2. Mouse hover on Top link using Actions class
  3. Then this will show the tooltip text
  4. Read the tooltip text using getText() method and store into String.
  5. Then using TestNG we can verify it.

Here syntax of Actions class for Mouse Hover operation.

 

Script for Handle Tooltip Text in Selenium using Actions Class

package datapkg;
import static org.testng.Assert.assertEquals;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.asserts.Assertion;

public class ActionClassExp {
    
    public static void main(String[] args) throws AWTException, InterruptedException {
        
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/css/css_tooltip.asp");
        driver.manage().window().maximize();
        
        //Scroll down the screen to see the tooltip element
        JavascriptExecutor script = (JavascriptExecutor)driver;
        script.executeScript("window.scrollBy(0,100)", "");
        Thread.sleep(2000);
        
        //xpath of element where we will get tooltip
        WebElement element = driver.findElement(By.xpath("//div[@class='w3-quarter'][1]/div"));
        
        //xpath of tooltip
        WebElement element1 = driver.findElement(By.xpath("//div[@class='w3-quarter'][1]/div/span"));
        
        // Creating Actions class object
        Actions ac = new Actions(driver);
        ac.moveToElement(element).perform();
        
        
        //Getting tooltip text using getText() method and store into String
        String tooltipText = element1.getText();
        
        //validating tooltip using assert method
        assertEquals(tooltipText, "Tooltip text");
        
        System.out.println(tooltipText);
        
           driver.quit();
        
        }
}

How To Capture Tooltip in Selenium using getAttribute

Sometimes tooltip text is present in title attributes in HTML. In this case we will use getAttributes(‘title’) to get the tooltip text.

Tooltip attributes

 

Code To Get Tooltip of an Element in Selenium

package datapkg;

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionClassExp2 {
    
    public static void main(String[] args) throws InterruptedException {
        
        
    System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.online.citibank.co.in/");
    driver.manage().window().maximize();
    
        
    //xpath of element where we will get tooltip
    WebElement element = driver.findElement(By.xpath("//a[@id='loginId']"));
    String tooltipText = element.getAttribute("title");
    
    
    System.out.println(tooltipText);
    
        driver.quit();
    }

}


In above script, first we will store xpath of element where tooltip is coming and store into a web element.

After this using getAttributes(“title”) method we can fetch tooltip text.


Watch this video:-


How to Take Screenshot of Tooltip in Selenium

Steps:

  1. Open given URL – https://www.online.citibank.co.in/
  2. Mouse hover on web element where tooltip appears.
  3. Using TakesScreenshot , we will capture screenshot of page where tooltip is present.

Code:

package datapkg;

import static org.testng.Assert.assertEquals;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import com.google.common.io.Files;

import org.apache.commons.io.FileUtils;

public class ActionClassExp2 {
    
    public static void main(String[] args) throws InterruptedException {
        
        
    System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.online.citibank.co.in/");
    driver.manage().window().maximize();
    
        
    //xpath of element where we will get tooltip
    WebElement element = driver.findElement(By.xpath("//a[@id='loginId']"));
    Actions ac = new Actions(driver);
    ac.moveToElement(element).perform();
    
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    
    FilesUtils.copyFile(scrFile, "C://test.png");
  
   	
       driver.quit();
    }

}


Summary:

I hope you enjoyed this tutorial and learnt something new.

Final word, Bookmark this post  ‘How to Handle Tooltip in Selenium for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

 

Must Read:

How To Select Multiple Checkbox in Selenium

How to Handle Dropdown without Select in Selenium

Leave a Comment