如何使用Java和Selenium WebDriver验证提示文本(tooltipText)

3
我有以下的HTML代码片段:
<div style="float:right; padding-right: 50px; margin-top: -10px;" id="divTooltips">
<img width="25px" height="25px" src="/mkteditor/css/images/tooltip.png" alt="">
</div>

当鼠标悬停在tooltip.png上时,会显示一个名为“help text at top place”的工具提示文本。我想在WebDriver中验证tooltipText。如何做到这一点?

请查看以下链接:http://stackoverflow.com/questions/8739989/not-able-to-get-tooltip-text-using-selenium-webdriver - some_other_guy
我尝试了以下代码: action.clickAndHold(elemByXPath("//[@id='divTooltips']/img")).perform(); String s = elemByXPath("//[@id='divTooltips']/img").getText(); System.out.println("Tooltip: " + s);当鼠标悬停在图片上时,工具提示文本被显示出来,但是在我的最后一行代码中没有任何文本被打印出来。 - Ripon Al Wasim
5个回答

3
每当你有工具提示时,"title"属性都会存在。(根据我的观察)
例如:转到http://www.linkedin.com/并查看顶部LinkedIn图像的HTML代码。
    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"));

谢谢你的回答。如果有title属性,获取工具提示就很容易了。但在我的情况下,没有title属性。这就是问题所在... - Ripon Al Wasim
1
在一个元素中有标题并不意味着工具提示会显示出来。 - Afshin Moazami

3
我的观察发现,每当您聚焦于一个元素并出现工具提示时,它都会显示在一个“div”中,该“div”在鼠标聚焦时变为可见状态。
以下是我建议的操作:
  1. 使用 actions 类将光标移动到要聚焦的元素上。
  2. Actions action = new Actions(driver); action.moveToElement('element_to_be_focused').build().perform();

  3. 由于工具提示显示一些文本,请读取该文本并在页面源代码 HTML 中找到相应的元素。
  4. 现在,简单地编写等待代码,以便等待具有工具提示文本的元素可见。
  5. new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.visibilityOfElementLocated('element_locator'));

  6. 使用 getText() 从工具提示元素中获取文本。
举个例子, 点击 这里 在给定的网站上,一旦我们将鼠标悬停在询问年龄的文本框上,我们就可以看到工具提示说“我们仅出于统计目的请求您的年龄。”
以下是获取工具提示文本的代码片段:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://jqueryui.com/tooltip/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[@id='age']"))).build().perform();
boolean isToolTipDisplayed = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).isDisplayed();
System.out.println("Is Tooltip displayed ? : " + isToolTipDisplayed);
            if (isToolTipDisplayed) {
                String tooltipText = driver.findElement(By.xpath("//div[@class='ui-tooltip-content']")).getText();
System.out.println("Tooltip Text:- " + tooltipText);
}
driver.switchTo().defaultContent();
driver.quit();

希望这能帮到你!

1
无需使用标题,根据要求进行更改。 - amogh

0
您可以使用以下内容来验证工具提示文本:
try{
    String tooltipText = "help text at top place";
    String tooltipTextToVerify = driver.findElement(By.id("divTooltips").getAttribute("title");
      if (tooltipText.equels(tooltipTextToVerify)){
         return true;
      else {return false;}
}

0
你可以尝试以下方法:
WebElement tooltipControl = driver.FindElement(By.id("divTooltips");
string titleText = tooltipControl.GetAttribute("textContent");

textContent 属性将提供工具提示中显示的文本。

希望这能帮到你。


0

你可以使用这个

WebElement element = driver.FindElement(By.id("divTooltips");
 string titleText = element.getAttribute("title");

现在标题存储在titleText字符串中。如果工具提示被隐藏并且您想查看其上的隐藏文本,请检查this

享受吧!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接