如何使用 getAttribute() 在 Selenium 中捕获工具提示?
我们可以使用 getAttribute 方法在 Selenium 中捕获工具提示。
此方法只能用于 HTML 代码中存在该属性的元素。
当我们悬停在元素上时,工具提示文本从元素中显示出来。要获取工具提示,我们必须将标题作为参数传递给 getAttribute 方法。
让我们看看一个带有工具提示的元素 UFSC Notes 的 HTML 代码。

在此,从 UPSC Notes 中显示的工具提示文本是 UPSC IAS Exams Notes -
TutorialsPoint 是为标题属性设置的值。
语法
WebElement l = driver.findElement(By.linkText("UPSC Notes"));
String a = l.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 TooltipAttribute{
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("UPSC Notes"));
//get title attribute
String a = l.getAttribute("title");
System.out.println("Tooltip obtained from title: " + a);
driver.quit();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP