Selenium WebDriver 的 isDisplayed() 方法如何工作?


我們可以使用 Selenium webdriver 中的**isDisplayed()**方法。此方法檢查 web 元素是否在頁面上可見。如果可見,則該方法返回真值,否則返回假值。

首先,我們必須使用 id、類、名稱、xpath 或 css 等定位符之一標識元素,然後在其上應用 isDisplayed() 方法。

語法

boolean s= driver.findElement(By.id("txt-bx")).isDisplayed();

讓我們檢查元素**關於 Tutorials Point 的職業生涯**是否顯示在頁面上。因為它可用,所以它將返回一個真值。

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class VisibleElement{
   public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://tutorialspoint.tw/about/about_careers.htm");
      // identify element
      WebElement p=driver.findElement(By.xpath("//h1"));
      //isDisplayed() to check if element visible
      boolean s= p.isDisplayed();
      System.out.println("The return value: " + s);
      driver.close();
   }
}

輸出

更新時間:2020-09-18

1K+ 瀏覽量

開啟您的 職業

完成課程即可獲得認證

開始
廣告