3 - Selenium元素定位和操作
2024-09-06 11:25:05
3.1定位
<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>
3.1.1 By.name("xxx")-定位name属性
driver.findElement(By.name("btnK")).click();
3.1.2 By.id()--按id定位
3.1.3 By.linkText()--定位超链接
<a href="/intl/en/about.html">About Google</a>
driver.findElement(By.linkText("About Google"));
//匹配部分文字
driver.findElement(By.partialLinkText("About"));
3.1.4 By.cssSelector()--速度快
3.1.5 By.xPath()--速度慢-终极方法
//F12控制台-->Element-->找到元素-->右键copy-->copy Xpath即可
dr.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/a[2]")).click();
3.2 元素操作
3.2.1 输入框
WebElement element = driver.findElement(By.id("passwd-id"));
//将输入框清空
element.clear();
//在输入框中输入内容:
element.sendKeys(“test”);
//获取输入框的文本内容:
element.getText();
3.2.2 下拉框-Select
Select select = new Select(driver.findElement(By.id("select")));
select.selectByVisibleText(“A”);
select.selectByValue(“1”);
select.deselectAll();
select.deselectByValue(“1”);
select.deselectByVisibleText(“A”);
select.getAllSelectedOptions();
select.getFirstSelectedOption();
3.2.3 多选框
WebElement checkbox = driver.findElement(By.class(".checkbox"));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
3.2.4 按钮
WebElement btn= driver.findElement(By.id("save"));
btn.click(); //点击按钮
btn.isEnabled (); //判断按钮是否enable
3.2.5 弹出框
Alert alert = driver.switchTo().alert();
//确定
alert.accept();
alert.dismiss(); //取消
alert.getText();//获取文本
最新文章
- DA - 信息获取途径汇总
- gcc参数-l传递顺序错误导致`undefined reference&#39;的一点小结
- CentOS 配置网络yum源
- mediawiki的安装与配置
- nn_slow和nn_fast
- animation of android (2)
- 小扩展大用处,自己扩展一个ForeachRead吧
- 【Unity Shaders】初探Surface Shader背后的机制
- iOS开发技巧系列---详解KVC(我告诉你KVC的一切)
- hdu1002大数相加
- 多线程、Socket
- 20170714_js闭包变量重置
- bzoj 3597: [Scoi2014]方伯伯运椰子
- dede后台出现 保存目录数据时失败,请检查你的输入资料是否存在问题
- 如何在同一台机器上安装多个MySQL的实例
- ios屏幕怎么投屏到电脑显示器
- TensorFlow中的通信机制——Rendezvous(二)gRPC传输
- MongoDB数据库简单操作
- Python:每日一题003
- 4.5Python数据类型(5)之列表类型
热门文章
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
- PCB设计基础及技巧
- C语言学习笔记--C语言中的逗号表达式
- nodejs PK php全方位比较PHP的Node.js的优缺点
- mongodb操作数据集合
- datanode与namenode的通信原理
- 5、预测和鉴定miRNA的靶基因
- asp.net刷新本页面的六种方法总结
- Linux 最小系统挂载U盘(SD、TF卡)并执行程序
- python 里 np.array 的shape (2,)与(2,1)的分别是什么意思,区别是什么?