(十五)下拉框选择

虫师 创建于 almost 7 years 之前

最后更新: 2 months 之前

阅读数: 1

(十五)下拉框选择


有时我们会碰到下拉框,WebDriver提供了Select类来处理下接框。

如百度搜索设置的下拉框,如下图: 搜索下拉框实现代码如下:

<select id="nr" name="NR">
  <option value="10" selected>每页显示 10 条</option>
  <option value="20">每页显示 20 条</option>
  <option value="50">每页显示 50 条</option>
<select>

操作下接框代码如下:

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;


public class SelectDemo {

  public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new ChromeDriver();
    driver.get("https://www.baidu.com");

    driver.findElement(By.linkText("设置")).click();
    driver.findElement(By.linkText("搜索设置")).click();
    Thread.sleep(2000);

    //<select>标签的下拉框选择
    WebElement el = driver.findElement(By.xpath("//select"));
    Select sel = new Select(el);
    sel.selectByValue("20");
    Thread.sleep(2000);

    driver.quit();
  }
}

Select类用于定位select标签。 selectByValue()方法符用于选取<option>标签的value值。

0

相关课程

测试工具合集
图文
测试工具合集

课程分类: 自动化测试 测试工具 中间件

我们收集的各种测试工具

  • 已完结
  • 已更新7集
  • 最后更新时间: 2024-03-18 12:04:41

免费

查看详情
selenium python 综合教程
图文
selenium python 综合教程

课程分类: 自动化测试

selenium python入门及提升

  • 已完结
  • 已更新21集
  • 最后更新时间: 2024-03-18 10:51:39

免费

查看详情
selenium javascript教程
图文
selenium javascript教程

课程分类: 自动化测试

通过selenium来入门js

  • 已完结
  • 已更新21集
  • 最后更新时间: 2024-03-18 10:29:34

免费

查看详情