[英]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. 当我将鼠标悬停在webelement上时,我试图找到工具提示文本,但无法获得工具提示文本。
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: 我能够将鼠标悬停在找不到工具提示文本HTML代码上:
<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. 您的代码有3个问题。
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
). 将ToolTipText
中的定位器固定为第一次调用时的定位器 : html/body/p[8]/a
(此xpath表示找到第八个p
的子代,而不是//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. 如果您使用保存此定位器的类参数(或改进的定位器,请参见示例)并使用它,那就更好了。 The title
attribute in your HTML is empty , so no wonder you get nothing. HTML中的title
属性为空 ,因此也就一无所获。
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. 如果它是工具提示的title
属性类型,而不是CSS生成的,则您无需将元素悬停即可获取工具提示。 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. 如果这是一个不平凡的工具提示(例如,当您将鼠标悬停在StackOverflow中时所用的工具提示),而不是像使用鼠标一样悬停在Actions
之后,则需要在HTML中找到该工具提示。
Example: 例:
private By tooltipElementBy = By.cssSelector("a.easyui-tooltip.tooltip-f");
String ToolTipText = driver.findElement(tooltipElementBy).getAttribute("title");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.