当前位置:网站首页>selenium 元素定位
selenium 元素定位
2022-07-05 07:18:00 【不负韶华ღ】
概述
一个页面最基本组成单元是元素,想要定位一个元素,我们需要特定的信息来说明这个元素的唯一特征。在selenium中,有8种定位到具体元素的方法。如下表所示:
| 定位器 Locator | 描述 |
|---|---|
| class | 定位class属性与搜索值匹配的元素(不允许使用复合类名) |
| css selector | 定位 CSS 选择器匹配的元素 |
| id | 定位 id 属性与搜索值匹配的元素 |
| name | 定位 name 属性与搜索值匹配的元素 |
| link text | 定位link text可视文本与搜索值完全匹配的锚元素 |
| partial link text | 定位link text可视文本部分与搜索值部分匹配的锚点元素。如果匹配多个元素,则只选择第一个元素。 |
| tag name | 定位标签名称与搜索值匹配的元素 |
| xpath | 定位与 XPath 表达式匹配的元素 |
方法
假设我们的网页由以下部分组成,这里尽可能使网页内容简单,来具体的介绍各个方法的不同。
<ol id="vegetables">
<li class="onions"><span>Onion is a Vegetable</span></li>
<li class="tomatoes"><span>Tomato is a Vegetable</span></li>
</ol>
<ul id="fruits">
<li class="apples"><span>Apple is a Fruit</span></li>
<li class="tomatoes"><span>Tomato is a Fruit</span></li>
</ul>
根据class属性定位
# 定位网页中class为tomatoes的元素,网页中符合条件的有两个,但是该方法只返回第一个
driver.find_element(By.CLASS_NAME, "tomatoes")
# 如果需要返回所有的元素,则需要使用find_elements方法
driver.find_elements(By.CLASS_NAME, "tomatoes")
根据id属性定位
# 在HTML种,由于id相同的两个元素不存在,故不需要find_elements方法
driver.find_element(By.ID, "fruits")
根据css选择器定位
# 在这种定位方式中,功能更加强大,定位更加灵活,但需要掌握css语法
driver.find_element(By.CSS_SELECTOR, '#fruits .tomatoes')
根据tag name定位
# 根据标签的名字,这里是根据<li></li>标签来定位的
driver.find_elements(By.TAG_NAME, "li")
根据name属性定位
name属性一般用于表单中,服务器通过name属性来获取具体元素的值。
# 查找name属性为inp的表单
driver.find_elements(By.NAME, "inp")
根据link text定位
该种定位方式需要元素的内容与输入的内容完全匹配。
# 定位文本为Tomato is a Vegetable的所有元素
driver.find_elements(By.LINK_TEXT, "Tomato is a Vegetable")
根据partial link text定位
该种定位方式只需要元素的内容与输入的内容部分匹配即可定位到相应元素。
# 定位文本中含有Tomato的所有元素
driver.find_elements(By.PARTIAL_LINK_TEXT, "Tomato")
根据xpath定位
# 根据xpath表达式来定位元素
driver.find_elements(By.XPATH, "/ol/li")
边栏推荐
- Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
- M2dgr slam data set of multi-source and multi scene ground robot
- npm install -g/--save/--save-dev的区别
- SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
- [node] NVM version management tool
- [vscode] search using regular expressions
- [software testing] 05 -- principles of software testing
- 【无标题】
- Rough notes of C language (1)
- Executealways of unity is replacing executeineditmode
猜你喜欢
随机推荐
Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
window navicat连接阿里云服务器mysql步骤及常见问题
Mid 2022 documentary -- the experience of an ordinary person
[software testing] 06 -- basic process of software testing
Concurrent programming - how to interrupt / stop a running thread?
PHY drive commissioning - phy controller drive (II)
Mipi interface, DVP interface and CSI interface of camera
CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
IPage can display data normally, but total is always equal to 0
U-Boot初始化及工作流程分析
并查集理论讲解和代码实现
网易To B,柔外刚中
SOC_ SD_ CMD_ FSM
Word import literature -mendeley
【软件测试】03 -- 软件测试概述
能量守恒和打造能量缺口
Oracle code use
I can't stand the common annotations of idea anymore
Course learning accumulation ppt
2022年PMP项目管理考试敏捷知识点(7)



![[untitled]](/img/d5/2ac2b15818cf66c241e307c6723d50.jpg)

![[vscode] prohibit the pylance plug-in from automatically adding import](/img/a7/d96c0c4739ff68356c15bafbbb1328.jpg)



