如何使用 Selenium java webdriver 選擇複選框?


我們可以使用 Selenium 來選擇複選框。在 html 文件中,每個複選框都有一個屬性 type,其值設定為複選框。為了選擇一個複選框,我們首先要用任何定位器識別出一個複選框,然後對其應用 click() 方法。

示例

程式碼實現方式。

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 CheckBoxSelect{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url ="https://tutorialspoint.tw/selenium/selenium_automation_practice.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element
      WebElement l=driver.findElement(By.xpath("//*[@value='Manual Tester']"));
      // select checkbox with click()
      l.click();
      driver.quit();
   }
}

輸出

更新日期:2020 年 8 月 28 日

2000 多次瀏覽

開啟你的 職業生涯

完成課程,獲取認證

開始學習
廣告