如何在 Selenium Webdriver 中获取工具提示文本?


借助 getAttribute 方法,我们可以获得 Selenium webdriver 中的工具提示文本。应将属性 title 作为此方法的参数传递。

仅当元素具有 title 属性时,才能应用此技术。

将鼠标悬停在该元素上时,会显示工具提示文本。在下面的 html 代码中,具有工具提示的元素具有属性 title,为 title 设置的值实际上是工具提示文本。

以下图片显示了菜单 Coding Ground,显示的工具提示文本为 - Coding Ground - 免费在线 IDE 和终端。

语法

WebElement m = driver.findElement(By.linkText("Coding Ground"));
String s = m.getAttribute("title");

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class ToolTipTxt{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://tutorialspoint.com/index.htm");
      // identify element
      WebElement l=driver.
      findElement(By.linkText("Coding Ground"));
      // get title attribute value
      String t = l.getAttribute("title");
      System.out.println("Retrieved tooltip text as :" +t);
      driver.quit();
   }
}

输出

更新于: 03-4 月-2021

5K+ 浏览

职业生涯起步

通过完成课程获得认证

开始
广告
© . All rights reserved.