Selenium Tooltip Mouseover Example
Tooltip is de facto standard nowadays for information sharing in web pages. In Selenium automation activities, we often encounter a scenario to verify tooltip text. So in this tutorial, we will see Selenium tooltip mouseover scenario to verify tooltip text. We will use Java in our example of verifying Google logo’s tooltip.
Tooltip appears on hovering mouse over any element. To simulate this scenario, we will be using Actions Interface of Java. Follow below steps to implement the Selenium tooltip verification.
- Declare an object of Actions class using below code.
1Actions ToolTip1 = new Actions(driver); - Locate Google logo.
1WebElement googleLogo = driver.findElement(By.xpath("//div[@id='hplogo']")); - Use ‘clickAndHold’ method of Actions class to simulate mouse hover action on Google logo.
1ToolTip1.clickAndHold(googleLogo).perform(); - Get tooltip value using ‘getAttribute’ command.
1String ToolTipText = googleLogo.getAttribute("title"); - After performing all above steps, your tooltip verification method should be displayed as below.
12345678public void GoogleTooltip() throws Exception {Actions ToolTip1 = new Actions(driver);WebElement googleLogo = driver.findElement(By.xpath("//div[@id='hplogo']"));ToolTip1.clickAndHold(googleLogo).perform(); // Perform mouse hover action using 'clickAndHold' methodString ToolTipText = googleLogo.getAttribute("title"); // Get the value of Tooltip by getAttribute commandAssert.assertEquals(ToolTipText, "Google");System.out.println("Tooltip value is: " + ToolTipText);} - Run your Selenium test.
We hope the above example of Selenium tooltip mouseover scenario would be helpful in your automation activities. Let us know how it goes for you in comments.
Comments
Hi,
I tried the above code for finding the tool tip for one of the fields in Gmail and it did not work for me..Please look in to my code and suggest
WebDriver driver = new FirefoxDriver();
driver.navigate().to(“https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default”);
WebElement we = driver.findElement(By.id(“GmailAddress”));
Actions toolTip = new Actions(driver);
toolTip.clickAndHold(we).perform();
System.out.println(we.getAttribute(“title”));
Please suggest.
Thanks,
Ranjith
**driver.navigate().to(“https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default”);
Comment out line 4.
The code still provides a ‘pass’ as the elements attribute ‘title’ always exists whether its being hovered over or not.
Unfortunately, this test is not asserting that the hover over function is working.