使用Selenium Webdriver選擇下拉選單選項的不同方法有哪些?


使用Selenium webdriver選擇下拉選單選項的方法有很多。這可以透過Select類來實現。HTML程式碼中的下拉選單由select標籤表示。

下拉選單中的選項由option標籤表示。此外,為了使用下拉選單,需要在程式碼中新增語句org.openqa.selenium.support.ui.Select。

從下拉選單中選擇選項的不同方法如下:

  • selectByIndex – 將下拉選單中要選擇的選項的索引作為引數傳遞。索引從0開始。

WebElement e = driver.findElement(By.className("opt"));
Select s = new Select(e);
s.selectByIndex(0);
  • selectByValue – 將下拉選單中要選擇的選項的value屬性作為引數傳遞。下拉選單中的選項應具有value屬性才能使用此方法。

WebElement e = driver.findElement(By.className("opt"));
Select s = new Select(e);
s.selectByIndex("option 1");
  • selectByVisibleText – 將下拉選單中要選擇的選項的可視文字作為引數傳遞。

WebElement e = driver.findElement(By.className("opt"));
Select s = new Select(e);
s.selectByVisibleText("Selenium");

讓我們嘗試從下面的下拉選單中選擇一個選項:

示例

使用selectByIndex的程式碼實現

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;
import org.openqa.selenium.support.ui.Select
public class DrpSelectByIndex{
   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/tutor_connect/index.php/");
      //identify dropdown
      WebElement n = driver.findElement(By.name("selType"));
      //select option by index
      Select s = new Select(n);
      s.selectByIndex(0);
      driver.quit();
   }
}

使用selectByValue的程式碼實現

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;
import org.openqa.selenium.support.ui.Select
public class DrpSelectByValue{
   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/tutor_connect/index.php/");
      //identify dropdown
      WebElement n = driver.findElement(By.name("selType"));
      //select option by value attribute of option
      Select s = new Select(n);
      s.selectByValue("subject");
      driver.quit();
   }
}

使用selectByVisibleText的程式碼實現

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;
import org.openqa.selenium.support.ui.Select
public class DrpSelectByVisibleTxt{
   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/tutor_connect/index.php/");
      //identify dropdown
      WebElement n = driver.findElement(By.name("selType"));
      //select option by value attribute of option
      Select s = new Select(n);
      s.selectByVisibleText("By Name");
      driver.quit();
   }
}

更新於:2021年4月6日

908 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告