How to get Tooltip Text in Selenium Webdriver

0 votes

How to get Tooltip Text in Selenium Webdriver?

Nov 30, 2020 in Selenium by anonymous
• 10,520 points

recategorized Nov 30, 2020 by Gitika 1,812 views

1 answer to this question.

0 votes

To get tooltip text in Selenium WebDriver, you typically interact with an element that displays a tooltip, and then extract the tooltip text. Tooltips are usually found in the title attribute of the HTML element, but they can also be implemented via JavaScript or CSS. 

Let's consider a scenario where you have a webpage with a button that displays a tooltip when hovered over. This tooltip provides additional information about the button. Our goal is to use Selenium WebDriver to automate the process of hovering over the button and retrieving the text from the tooltip.

Scenario:

  • Webpage URL: https://www.edureka.co 

  • There is a button on the page with the ID submit-button.

  • When you hover over this button, a tooltip appears.

  • The tooltip is implemented using the title attribute of the button.

Objective:

Write a Selenium WebDriver script to hover over the submit-button and extract the tooltip text.

Implementation Steps:

  • Set Up WebDriver: Initialize the WebDriver for the browser you are testing on (e.g., Chrome, Firefox).

  • Navigate to the Webpage: Open the webpage where the button is located.

  • Locate the Button: Use WebDriver to find the button element by its ID.

  • Retrieve Tooltip Text: Since the tooltip in this scenario is implemented using the title attribute, get the value of this attribute from the button element.

Handle Complex Tooltip (if needed): If the tooltip is not based on the title attribute and requires interaction like hovering, use the Actions class to simulate the hover action and then retrieve the tooltip text.

Here's how this could be implemented in Java

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


public class TooltipExample {

 public static void main(String[] args) {

 // Set up the driver (make sure to specify the correct path to your driver)

 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

 WebDriver driver = new ChromeDriver();


 // Open the webpage

 driver.get("https://www.edureka.co");


 // Find the button by its ID

 WebElement button = driver.findElement(By.id("submit-button"));


 // Retrieve the tooltip text

 String tooltipText = button.getAttribute("title");


 System.out.println("Tooltip text: " + tooltipText);


 // Close the browser

 driver.quit();

 }

}

In this script, driver.findElement(By.id("submit-button")) locates the button, and button.getAttribute("title") retrieves the tooltip text. This script assumes a simple tooltip implemented via the title attribute. If the tooltip is more complex, you might need to use the Actions class to perform a hover action before retrieving the tooltip text.

.

answered Nov 30, 2020 by Gitika
• 65,910 points

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers
0 votes
0 answers

How to get the text from the HTML5 input error message in Selenium?

On giving invalid data in the email ...READ MORE

Mar 12, 2019 in Selenium by Vaishnavi
• 1,180 points
1,752 views
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,747 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,619 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,696 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,560 views
+1 vote
4 answers

How to get typed text from a textbox by using Selenium Webdriver?

Hey Ashmita, to get the typed text ...READ MORE

answered Jun 25, 2019 in Selenium by Abha
• 28,140 points
38,375 views
0 votes
6 answers

How to specify "ENTER" button functionality in Selenium WebDriver code?

using OpenQA.Selenium.Interactions; Actions builder = new Actions(driver); ...READ MORE

answered Feb 13, 2019 in Selenium by anonymous
94,824 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP