简体   繁体   中英

Selenium webdriver tooltip text on mouse over

I am trying to find tooltip text when I mouse hover webelement, but I am not able to get the tooltip text.

Code:

Actions a=new Actions(driver);  
a.moveToElement(driver.findElement(By.xpath("html/body/p[8]/a"))).build().perform();
String ToolTipText = driver.findElement(By.xpath("html/body/p[8]//a")).getAttribute("title");
System.out.println(" tool  "+ToolTipText);

I am able to hover over just not able to find tooltip text HTML code:

<p>
The tooltip can use each elements title attribute.
<a class="easyui-tooltip tooltip-f" title="" href="#">Hover me</a>
 to display tooltip.
</p>

Please help.

Thanks.

There are 3 problems with your code.

  1. Fix your locator in the ToolTipText to the same one as in your first call: html/body/p[8]/a (this xpath says find the children of the eighth p and not any descendant as in //a ). It would be even better if you use a class parameter that holds this locator (or an improved locator, see example) and use it.
  2. The title attribute in your HTML is empty , so no wonder you get nothing.

  3. If it's a title attribute type of tooltip and not one that's generated with CSS, then you don't need to hover the element to get the tooltip. If it's a non trivial tooltip, like the one when you hover a tag in StackOverflow, than after hovering with Actions like you did, you need to find the tooltip in the HTML.

Example:

private By tooltipElementBy = By.cssSelector("a.easyui-tooltip.tooltip-f");

String ToolTipText = driver.findElement(tooltipElementBy).getAttribute("title");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM