Selenium下拉框自带方法使用详解。

public void SelectOptionSelenium() {
driver.get("https://www.imooc.com/user/setprofile");
driver.findElement(By.className("js-edit-info")).click();//点击编辑按钮
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement UserFrom = driver.findElement(By.id("profile"));//定位到from表单
WebElement job = UserFrom.findElement(By.id("job"));//通过from表单定位到职位下拉框
/**
* 定位到下拉框后,select.selectByIndex()方法进行选择
*/
Select select = new Select(job);
select.selectByIndex(8);//根据下标选择
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
select.selectByValue("1");//根据职位名称对应的value值选择
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
select.selectByVisibleText("PHP开发工程师");//根据职位名称选择
System.out.println(select.isMultiple());//判断下拉框是否是多选的
// select.deselectByVisibleText("PHP开发工程师");//不选中这个方法针对的都是多选下拉框
java.util.List<WebElement> SelectOption = select.getAllSelectedOptions();//获取到下拉框里面被选择的所有职位
for (WebElement option : SelectOption) {
System.out.println(option.getText());
}
driver.close();
}

posted @ 2020-10-15 21:49  一块  阅读(227)  评论(0编辑  收藏  举报