This article will help you to understand How to Capture Tooltip in Selenium Webdriver and how we can verify the same in our automated test.
Tooltip sometime is also known as Help text.
When we use mouse hover on some element or click on some element then this tooltip comes. Here is a quick example of help text or tooltip.
Example- Example of Gmail, when we create a new account and fill some fields than for elements you will get the warning or help text.
Approach to capture tooltip message in Selenium
- In this example first, open Gmail account.
- Click on username then tooltip message will appear.
- Then mouse hover on the message
- Capture that message in a String variable and print.
- Then use TestNG Assertion to verify messages.
Program to implement-How to Capture Tooltip in Selenium Webdriver
package demo1; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Tooltip { public static void main(String[] args) { // This will open browser WebDriver driver=new FirefoxDriver(); // This will maximize your browser driver.manage().window().maximize(); // Open gmail account creation page driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default"); // Click on username textbox driver.findElement(By.xpath(".//*[@id='GmailAddress']")).click(); // Create action class object Actions builder=new Actions(driver); // find the tooltip xpath WebElement username_tooltip=driver.findElement(By.xpath("html/body/div[2]/div[1]")); // Mouse hover to that text message builder.moveToElement(username_tooltip).perform(); // Extract text from tooltip String tooltip_msg=username_tooltip.getText(); // Print the tooltip message just for our refrences System.out.println("Tooltip/ Help message is "+tooltip_msg); } }
Output-
How to Capture Tooltip in Selenium
package demo1; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.Assert; public class Tooltip { public static void main(String[] args) { // This will open browser WebDriver driver=new FirefoxDriver(); // This will maximize your browser driver.manage().window().maximize(); // Open Gmail account creation page driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default"); // Click on username textbox driver.findElement(By.xpath(".//*[@id='GmailAddress']")).click(); // Create action class object Actions builder=new Actions(driver); // find the tooltip/helptext message xpath WebElement username_tooltip=driver.findElement(By.xpath("html/body/div[2]/div[1]")); // Mouse hover to that text builder.moveToElement(username_tooltip).perform(); // Extract the text from tooltip using getText String tooltip_msg=username_tooltip.getText(); // Print the tooltip message System.out.println("Tooltip message is "+tooltip_msg); // This is expected message should come and store in variable String expected_tooltip="You can use letters, numbers, and periods."; // It will compare if actual matches with expected then TC will fall else it will fail Assert.assertEquals(tooltip_msg, expected_tooltip); System.out.println("Message verifed"); } }
Output
Thanks for visiting my blog. Please comment in case any issue.
Keep in touch. Have a nice day 🙂
Tejas says
Hii
I am unable to get the text of tooltip tried with above code. Selenium is not printing anything in the console. Below is my code
Actions actions = new Actions(driver);
WebElement menuOption = driver.findElement(By.xpath(“//*[@id=’reminderEmailSentDetails-A111′ and @class=’ReminderEmailSentIcon’]”));
actions.moveToElement(menuOption).perform();
WrapperFunctionUtilities.waitForTime(4000);
System.out.println(“Done Mouse hover on reminder sent icon”);
String expectedstring = menuOption.getText();
Log.info(“Details are”+expectedstring);
Selenium is only printing Details are in the console
Mukesh Otwani says
Hi Tejas,
Please try in debug mode so that you will get exact details like what is the issue.
Thanks
Mukesh
Mukesh Otwani says
Ramesh, it does not matter whether it is div tag, span tag. The concept is we need to capture text and validate the date.
Jayesh says
Great article Mukesh !!!
I’m facing challenge in handling captcha in one of my proj, can u suggest some tricks and techniques to handle the same.
Mukesh Otwani says
Hi Jayesh,
Please go through the below links
https://www.youtube.com/watch?v=0pjzLMbtimk
Hope that these will clarify your doubts
SATVEER BAJWA says
Hi Mukesh,
I am trying to automate the tooptip message after hover the mouse on it , but I am not able to do it. Have some questions:
1. Do we need to get the xpath of the tool right where user hover the mouse not the location where tooltip message will show? because in my webpage here is icon let say edit after hover the mouse on it shows tooltip message then I need to give xpath of edit icon right?
Mukesh Otwani says
Hi Satveer,
Selenium always works on DOM elements. If you want to get text for tooltip, somehow you need to get corresponding locator on DOM based on which you can webdriver operation. Here first you need to mouse hover over required control on screen then wait for few seconds then check for locator property for tooltip.
SATVEER BAJWA says
Hi Mukesh,
Thanks for your reply. Yes it works now, the script ran successfully but when the output printed in console that is only “Tooltip/ Help message is” . it doesn’t extract the message (+tooltip_msg1). or one more thing if we have multiple hover functions on the same page then I used same Create action class object Actions builder=new Actions(driver) multiple times . it ran successfully. is it correct?
System.out.println(“Tooltip/ Help message is “+tooltip_msg6);
Mukesh Otwani says
Hi Satveer,
If you are using same Actions class object then its good only.
SATVEER BAJWA says
Okay Thank you so much Mukesh. Please provide me solution for this the script ran successfully but when the output printed in console that is only “Tooltip/ Help message is” . it doesn’t extract the message (+tooltip_msg1).
Thanks
Mukesh Otwani says
Hi Satveer,
Now a days, market is full of different types of ui components and libraries so without looking and working on web ui of application, I can’t provide any solid solution for this.
Suresh says
I found every time tool tip changes in DOM as it loads after clicking on text box in browser, so i used below , it works now
// find the tooltip xpath
WebElement username_tooltip=driver.findElement(By.xpath(“//div[contains(@id,’bubble’)]”));
Mukesh Otwani says
Cheers Suresh
Paras says
Hi Mukesh,
How to find the xpath of tool tip item, how did u do that ?
Mukesh Otwani says
use Mouse Hover and for xpath use firepath.
Manoj Kumar says
Hello sir
How to get the path for the tool-tip, I am not being able to locate it with the help of firebug or fire path?
Raveendra says
1) First Navigate to the Create Gmail account page
2) using the xpath locate the CHOOSE YOUR USERNAME CheckBox By using the Firebug and perform click operation.
driver.findElement(By.xpath(“//input[@id=’GmailAddress’]”)).click();
3)After Clicking CheckBox u will get the Tool Tip Message
4) locate the toolTip Meaage Box using The Xpath Directly or else using MouseHover ,
Here i ve used the some Explicitwait
WebDriverWait wait = new WebDriverWait(driver,5);
WebElement Text;
Text = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(“html/body/div[3]”)));
Thread.sleep(2000);
System.out.println(Text.getText());
For the time being i have used Explicitwait
Hope it will helps you
Mukesh Otwani says
THanks Raveendra 🙂
krishnaveni says
Hi mukesh,
i just tried the tooltip code that you mentioned above. its working well.
As well i tried with out using action class and that works well.
so what is the need of using action class over here,can you please clarify..
Thank you for the blog,which helps a lot
you can see the code below…
WebDriver driver = new FirefoxDriver();
//Gmail account creation page
driver.get(“https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default”);
driver.manage().window().maximize();
//click on user name filed
driver.findElement(By.xpath(“.//*[@id=’GmailAddress’]”)).click();
// create Action class object
//Actions builder= new Actions(driver);
//find the tool tip message xpath
WebElement tooltip = driver.findElement(By.xpath(“html/body/div[3]”));
//Mouse hoover to that text message
//builder.moveToElement(tooltip).perform();
//get the text
String tooltip_message = tooltip.getText();
System.out.println(“tooltip message here” + tooltip_message);
Mukesh Otwani says
Hi krishnaveni,
Good to know it worked for you. We used Actions class for Mouse hover 🙂