Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify tooltipText in Selenium WebDriver using Java

I have the following snippet of HTML code:

<div style="float:right; padding-right: 50px; margin-top: -10px;" id="divTooltips">
<img width="25px" height="25px" src="/mkteditor/css/images/tooltip.png" alt="">
</div>

During mouse over tooltip.png a tooltip text "help text at top place" is displayed. I want to verify the tooltipText in WebDriver. How to do it?

like image 649
Ripon Al Wasim Avatar asked Dec 04 '25 12:12

Ripon Al Wasim


2 Answers

Whenever you will have tooltip then "title" attribute will be there. (as per my observation ).

for example: Goto http://www.linkedin.com/ and view HTML code for LinkedIn image on top. LinkedIn

    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://www.linkedin.com/");
    WebElement onElement = driver.findElement(By.xpath("html/body/div[1]/div[1]/div/h2/a"));
    System.out.println("Tooltip : " + onElement.getAttribute("title"));
like image 126
Anand Somani Avatar answered Dec 06 '25 02:12

Anand Somani


My observation says, whenever you focus on element and a tool-tip appears it is displayed inside a "div" which is made visible on mouse focus.

Here is something i would suggest,

  1. Move to element using actions class

    Actions action = new Actions(driver); action.moveToElement('element_to_be_focused').build().perform();

  2. Now since tooltip displays some text, read that text and find out element from page source HTML where it is mentioned.

  3. Now simply write code to wait for visibility of element having tooltip text.

    new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated('element_locator'));

  4. Get text from tooltip element using getText()

To give an example, Click here

On given website once we mouse hover to textbox asking for age, we can see tooltip saying 'We ask for your age only for statistical purposes.'

Following is code snippet to get tooltip text:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://jqueryui.com/tooltip/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@id='age']"))).build().perform();
boolean isToolTipDisplayed = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).isDisplayed();
System.out.println("Is Tooltip displayed ? : " + isToolTipDisplayed);
            if (isToolTipDisplayed) {
                String tooltipText = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).getText();
System.out.println("Tooltip Text:- " + tooltipText);
}
driver.switchTo().defaultContent();
driver.quit();

Hope this will help you !

like image 36
Sameer Patil Avatar answered Dec 06 '25 02:12

Sameer Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!