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.
.