Selenium 的 Select 类中有哪些不同的方法?


Selenium 的 Select 类中可用的各种方法如下所示:

  • selectByVisibleText(String args)

    此方法最常用于下拉列表。使用此方法,在下拉列表和多选框中选择选项非常简单。它以字符串作为参数,不返回值。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.selectByVisibleText("Selenium");
  • selectByIndex(String args)

    此方法获取下拉列表中要选择的选项的索引。它以 int 作为参数,不返回值。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.selectByIndex(1);
  • selectByValue(String args)

    此方法获取下拉列表中要选择的选项的值。它以字符串作为参数,不返回值。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.selectByValue(“Testing”);
  • getOptions()

    此方法以 Web 元素列表的形式获取 select 标签下所有选项。它没有参数。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.getOptions();
  • deSelectAll()

    此方法用于多选下拉列表,并取消所有选定的选项。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp >>")));
    s.deSelectAll();
  • deselectByVisibleText(String args)

    此方法用于根据与参数类似的显示文本,取消选择下拉列表中的所有选项。它以字符串作为参数,不返回值。此方法用于多选下拉列表。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.deselectByVisibleText("Selenium");
  • deselectByIndex(String args)

    此方法获取要取消选择下拉列表中选项的索引。它以 int 作为参数,不返回值。此方法用于多选下拉列表。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.deselectByIndex(1);
  • deselectByValue(String args)

    此方法获取要取消选择下拉列表中选项的值。它以字符串作为参数,不返回值。此方法用于多选下拉列表。

    语法:

    Select s = new Select(driver.findElement(By.id("<< id exp>>")));
    s.deselectByValue(“Testing”);

更新于: 2020年6月11日

914 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.