Retrieve Tool tip text from Selenium WebDriver in Java

I have written below code to retrieve tooltip text from an application. But it’s always passing the value as empty.(“”)
Assume the application is ToolsQA “http://demoqa.com/” and I want to take the tooltip text of the http://demoqa.com/tooltip/ “Your age”

Please see the code below which I used.

public String  getToolTipText(String xpath) throws InterruptedException{

        xpath = "//div[@class='inside_contain']//input";
        WebElement mainElement = driver.findElement(By.xpath(xpath));
        Actions act = new Actions(driver);
        //act.moveToElement(mainElement).build().perform();
        act.clickAndHold(mainElement).build().perform(); 
        Thread.sleep(5000);
        String tooltiptextreal = mainElement.getAttribute("title");

        return tooltiptextreal;
    }

Assertion:

String TooltipText = "We ask for your age only for statistical purpose.";
    Assert.assertEquals(pToolsQAWidgetTooltipPageDom.getToolTipText(), TooltipText);

Output:

java.lang.AssertionError: expected [We ask for your age only for statistical purpose.] but found []

Here is the Answer to your Question:

To retrieve the Tooltip text We ask for your age only for statistical purpose. you can use the following code block. The following code block extracts the Tooltip as We ask for your age only for statistical purpose. and prints on the console.

package demo;

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.annotations.Test;

public class Q45433269_Tooltip 
{
    @Test
    public void  getToolTipText() throws InterruptedException
    {
        System.setProperty("webdriver.gecko.driver", "C://Utility//BrowserDrivers//geckodriver.exe");
        WebDriver driver =  new FirefoxDriver();
        driver.navigate().to("http://demoqa.com/tooltip/");
        driver.findElement(By.id("age")).click();
        Actions act = new Actions(driver);
        WebElement age_purpose = driver.findElement(By.xpath("//div[contains(@id, 'ui-id-')]/div[@class='ui-tooltip-content']"));
        act.moveToElement(age_purpose).perform();
        String tooltiptextreal = age_purpose.getText();
        System.out.println("Tooltip text is : "+tooltiptextreal);
    }
}

The output on the console is as:

Tooltip text is : We ask for your age only for statistical purposes.
PASSED: getToolTipText

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

Let me know if this Answers your Question.

When you get your mouse over that input the “title” attribute gets removed (check with browser dev tool) so that you cannot obtain the proper value.

If you need “title” attribute value try removing

Actions act = new Actions(driver);
//act.moveToElement(mainElement).build().perform();
act.clickAndHold(mainElement).build().perform(); 

from your code.