Selenium+java – 下拉框处理

  • 2019 年 10 月 3 日
  • 笔记

?????????????????????????????????????????????????????????????

1?Select?????????????

  • select.selectByIndex # ??????
  • selectByValue # ??value???
  • selectByVisibleText # ?????????

?????

  • index????“0”???
  • value?option???value??????
  • VisibleText??option???????????

2?Select???????options?????

  • getOptions() # ??select?????options
  • getAllSelectedOptions() # ??select???????????
  • getFirstSelectedOption() # ??select???????????

3?Select?????????????

  • select.deselectAll() # ?????????
  • deselectByIndex() # ?????????
  • deselectByValue() # ??????value?
  • deselectByVisibleText() # ?????????

4???????

<!DOCTYPE html>  <html lang="en">  <head>      <meta charset="UTF-8">      <title>Select??????</title>  </head>  <body>    <h4>????????</h4>  <select id="select">      <option value="1">??</option>      <option selected="selected" value="2">??</option>      <option value="3">??</option>      <option value="4">?</option>  </select>  </body>  </html>

?????????

package com.brower.demo;    import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.WebElement;  import org.openqa.selenium.chrome.ChromeDriver;  import org.openqa.selenium.support.ui.Select;  import org.testng.annotations.AfterClass;  import org.testng.annotations.BeforeClass;  import org.testng.annotations.Test;    import java.util.concurrent.TimeUnit;    public class TestSelectDemo {        WebDriver driver;        @BeforeClass      public void beforeClass() {          System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");          driver = new ChromeDriver();      }        @Test      public void testSelectDemo() {          //??????          driver.get("file:///C:/Users/Administrator/Desktop/SelectDemo.html");          driver.manage().window().maximize();          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);          //??select????          WebElement element = driver.findElement(By.id("select"));          Select select = new Select(element);          //??????,???1???:??          select.selectByIndex(0);          //??value????4???:?          select.selectByValue("4");          //????????2???:??          select.selectByVisibleText("??");          //????????          System.out.println(select.isMultiple());        }        @AfterClass      public void afterClass() {          driver.quit();      }  }

??????select??????????????????????????????