Java的Selenium的特殊元素操作与定位之select下拉框

如果页面元素是一个下拉框,我们可以将此web元素封装为Select对象

  • Select select=new Select(WebElement element);

Select对象常用api

  • select.getOptions();//获取所有选项
  • select.selectBylndex(index);//根据索引选中对应的元素
  • select.selectByValue(value);//选择指定value值对应的选项
  • select.selectByVisibleText(text);//选中文本值对应的选项
java 复制代码
//select下拉框处理
chromeDriver.get("http://www.baidu.com");
chromeDriver,findElement (By.xpath("//div[@id='u1']/a[text()='设置']")).click();
chromeDriver.findElement (By.xpath("//a[text()='高级搜索']")).click();
Thread.sleep(2000);
//定位时间下拉框
WebElement webElement = chromeDriver.findElement (By.name("gpc"));
//把WebElement封装成Select对象
Select select =new Select(webElement);1
//select下拉框 索引值从0开始
select.selectByIndex(1);
Thread.sleep(2000);
select.selectByVisibleText("最近一月");
相关推荐
karry_k11 小时前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k11 小时前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking15 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩18 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
荣码19 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
plainGeekDev21 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波1 天前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯1 天前
GoF设计模式——备忘录模式
java·后端·spring·设计模式