如何在 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.tw/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.