• Selenium Video Tutorials

Selenium WebDriver - 多选



Selenium Webdriver 可以使用 Select 类在网页上的下拉菜单中进行多选操作。网页上的下拉菜单可以分为两种类型 - 单选和多选。

多选下拉菜单由名为 select 的标签名标识。此外,其每个选项都由名为 option 的标签名标识。除此之外,多选下拉菜单应具有一个名为 multiple 的属性。

HTML 中多选下拉菜单的识别

右键单击网页,然后在 Chrome 浏览器中单击“检查”按钮,结果将显示整个页面的 HTML 代码。要检查页面上的多选下拉菜单,请单击可见 HTML 代码顶部可用的向左向上箭头,如下所示。

Selenium Multi Select 1

一旦我们单击并将箭头指向文本多选下拉菜单旁边的下拉菜单,其 HTML 代码就会可见,反映了 select 标签名(括在<>中),以及 option 标签名内的选项及其属性 multiple。

Selenium Multi Select 2

Select 中的基本多选方法

Selenium webdriver 的 Select 类中提供了多种方法,可以帮助我们处理这两种多选下拉菜单

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

让我们再举一个以下页面的例子,在这里我们将访问文本多选下拉菜单旁边的多选下拉菜单,选择值电影、音乐和游戏、电子产品和电脑以及书籍,并使用上面讨论的方法执行一些验证。

Selenium Multi Select 3

代码实现

package org.example; 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 java.util.List; import java.util.concurrent.TimeUnit; public class HandlingMultDropdown { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will get dropdown driver.get("https://tutorialspoint.com/selenium/practice/select-menu.php"); // identify multiple dropdown WebElement dropdown = driver.findElement(By.xpath("//*[@id='demo-multiple-select']")); // object of Select class Select select = new Select(dropdown); // gets options of dropdown in list List<WebElement> options = select.getOptions(); for (WebElement opt : options){ System.out.println("Options are: " + opt.getText()); } // return true if multi-select dropdown Boolean b = select.isMultiple(); System.out.println("Boolean value for multiple dropdown: "+ b); // select item by visible text select.selectByVisibleText("Books"); // select item by value select.selectByValue("2"); // select item by index select.selectByIndex(2); // get all selected options of dropdown in list List<WebElement> selectedOptions = select.getAllSelectedOptions(); for (WebElement opt : selectedOptions){ System.out.println("Selected Options are: " + opt.getText()); } // get first selected option in dropdown WebElement f = select.getFirstSelectedOption(); System.out.println("First selected option is: "+ f.getText()); // deselect option by index select.deselectByIndex(2); // get all selected options of dropdown after deselecting 1 List<WebElement> deletedOptions = select.getAllSelectedOptions(); System.out.println("No. options selected after deselecting one option: " + deletedOptions.size()); // deselect all selected items select.deselectAll(); // get all selected options of dropdown after deselected List<WebElement> deletedAllOptions = select.getAllSelectedOptions(); System.out.println("No. options selected after deselecting all: " + deletedAllOptions.size()); // Closing browser driver.quit(); } }

输出

Options are: Books
Options are: Movies, Music & Games
Options are: Electronics & Computers
Options are: Home, Garden & Tools
Options are: Health & Beauty
Options are: Toys, Kids & Baby
Options are: Clothing & Jewelry
Options are: Sports & Outdoors
Boolean value for multiple dropdown: true
Selected Options are: Books
Selected Options are: Movies, Music & Games
Selected Options are: Electronics & Computers
First selected option is: Books
No. options selected after deselecting one option: 2
No. options selected after deselecting all: 0

Process finished with exit code 0

在上面的示例中,我们使用控制台中的消息获取了下拉菜单的所有选项 - 选项为:书籍,选项为:电影、音乐和游戏,选项为:家居、园艺和工具,选项为:健康和美容,选项为:玩具、儿童和婴儿,选项为:服装和珠宝,选项为:运动和户外

我们还使用控制台中的消息验证了下拉菜单具有多个选择选项 - 用于检查的布尔值为:true。我们使用控制台中的消息检索了下拉菜单中的选定选项 - 选定的选项为:书籍,选定的选项为:电影、音乐和游戏,以及选定的选项为:电子产品和电脑

我们还使用控制台中的消息获取了第一个选定的选项 - 第一个选定的选项为:书籍。取消选择一个选定的选项后,我们使用控制台中的消息收到了选定选项的数量 - 取消选择一个选项后选定的选项数:2

最后,我们取消选择了下拉菜单中所有选定的选项,因此在控制台中获得了消息 - 取消选择所有选项后选定的选项数:0

最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。

这总结了我们对 Selenium Webdriver - 多选教程的全面介绍。我们从描述 HTML 中多选下拉菜单的识别、Select 中的基本多选方法以及一个示例来说明如何在 Selenium Webdriver 中处理多选下拉菜单开始。

这为您提供了 Selenium Webdriver - 多选的深入知识。明智的做法是不断练习您所学到的知识,并探索与 Selenium 相关的其他知识,以加深您的理解并拓宽您的视野。

广告