1.下拉选择框
1.1 Select类是什么?
Select类是WebDriver为解决select标签定位诞生的,此类定位的是select标签!
1.2 Select类的方法
- selectByIndex():根据option索引来定位,从0开始
- selectByValue():根据option属性的value值进行定位
- selectByVisibleText():根据文本值实现定位
1.3 需求
- 打开百度首页,鼠标移动到"设置"选项
- 等待2秒钟,在"设置"的下拉菜单中,点击"搜索设置"选项
- 等待2秒钟,"搜索结果显示条数"选项,选择"每页显示50条",等待2秒,选择"每页显示20条"
- 等待2秒钟,关闭浏览器窗口
1.3.1 根据option索引来定位
完整代码如下:
package com.taobaoxue.day01;
import java.util.concurrent.TimeUnit;
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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
/**
* WebDriver模拟下拉选择框操作
*
* selectByIndex():根据option索引来定位,从0开始
*
* @author HW
*
* @date 2020年3月17日 下午7:04:12
*
*/
public class SelectDemo {
public static void main(String[] args) throws InterruptedException {
// 指定chromedriver驱动的路径
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\HW\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
// 实例化chrome浏览器