0

I apologize if I'm repeating this question. I've been through a lot of answers on this site but I'm still not getting the tool-tip text. I can read the title if I don't use the Actions class to hover-over and display the tool-tip. However once I use the Actions class to display the tool-tip then title is always empty. I don't want to get the text before I hover-over, isn't the whole idea to read the tool-tip text that is displayed?

driver.get("https://jqueryui.com/tooltip/");


WebElement frame = driver.findElement(By.xpath("//iframe[@src='/resources/demos/tooltip/default.html']"));
driver.switchTo().frame(frame);

WebElement element = driver.findElement(By.id("age"));
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();  (I've also tried clickAndHold method)
WebElement toolTip = driver.findElement(By.xpath("//*[@id='age']"));

// To get the tool tip text and assert
String toolTipText = toolTip.getAttribute("title");
System.out.println("toolTipText-->"+toolTipText);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
zara
  • 99
  • 1
  • 8

1 Answers1

1

Tooltips are extracted only after mouse over is performed.

To print the Hover the field to see the tooltip. as the desired element is within a <iframe> you need to:

  • scrollIntoView() the desired iframe

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for visibilityOfElementLocated() of the element you need to Mouse Hover.

  • Induce WebDriverWait for visibilityOfElementLocated() for the element from where you need to retrieve the tooltip:

  • You can use the following based Locator Strategies:

    System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver =  new ChromeDriver(options);
    driver.get("https://jqueryui.com/tooltip/");
    ((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[@class='entry-title']"))));
    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='/resources/demos/tooltip/default.html']")));
    new Actions(driver).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='age']")))).build().perform();
    System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='age']//following::div[text()]"))).getText());
    
  • Console Output:

    We ask for your age only for statistical purposes.
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for the quick response. However this is still incorrect text."Hover the field to see the tool-tip." is not the tool-tip text, tool tip text is "We ask for your age only for statistical purposes. – zara Jul 22 '20 at 14:29
  • 1
    @zara that was my bad, fixed it. Retest and let me know the status. – undetected Selenium Jul 22 '20 at 14:48
  • Thanks, yes this worked. Last question: I see the tool tip text "We ask for your age only for statistical purposes." on multiple places in the DOM. How did you know to get the text from By.xpath("//input[@id='age']//following::div[text()]". I'd been trying to get the text by using driver.findElement(By.xpath("//input[@id='age']")).getAttribute("title"); (obviously I was wrong)
    We ask for your age only for statistical purposes.
    – zara Jul 22 '20 at 18:22
  • 1
    @zara The tooltip texts are in the following `
    ` which becomes visible only when you mouse over the textbox which fires an event.
    – undetected Selenium Jul 22 '20 at 18:22
  • Thanks, that makes sense – zara Jul 24 '20 at 16:33