如何使用selenium提取工具提示内容,其中工具提示是由javascript动态加载的

How do i Extract tooltip content using selenium where tooltip is dynamically loading by javascript?

本文关键字:工具提示 javascript 动态 加载 selenium 何使用 提取      更新时间:2023-09-26

我正在测试一个web应用程序,我需要验证在将鼠标移动到图形的一部分上时显示的工具提示的内容。

这段html代码是这个技巧的来源:

<div id="area-serverSlot-6a" class="annotation" style="width: 21px; height: 38px; left: 186px; top: 117px; position: absolute; z-index: 20; cursor: pointer;" name="annotation" present="1" tooltip="" href="javascript:select_module('server', '6a');" onareaover="javascript:DisplayTip('serverSlot-6a', TITLE,'Server Slot 6a')" onareaout="javascript:UnTip()"></div>

当工具提示随着鼠标移动时,我无法将鼠标移动到工具提示上方。请建议一些方法来获取工具提示的内容。

下面是DisplayTip的代码:
function DisplayTip()
{
var titleType = arguments[2];
var slotNumPos = titleType.lastIndexOf(' ');
var slotNum = " UNKNOWN";
var title = "UNKNOWN";
if (titleType.indexOf('Server') >= 0) title = vServerSlot;
else if (titleType.indexOf('IOM') >= 0) title = vIOMSlot;
else if (titleType.indexOf('PSU') >= 0) title = vPSUSlot;
else if (titleType.indexOf('CMC') >= 0) title = vCMCSlot;
else  if (titleType.indexOf('KVM') >= 0) title = vKVMSlot;
else if (titleType.indexOf('Fan') >= 0) title = vFanSlot;
else if (titleType.indexOf('LCD') >= 0) title = vLcdSlot;
if (slotNumPos >= 0)
{
slotNum = titleType.substring(slotNumPos);
title += slotNum;
}
TagToTip(arguments[0], TITLE, title, JUMPHORZ, true,
JUMPVERT, true, ABOVE, true);
} 

Nick,

我以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"));