当前位置:网站首页>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")
边栏推荐
- 1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
- [vscode] prohibit the pylance plug-in from automatically adding import
- Jenkins reported an error. Illegal character: '\ufeff'. Class, interface or enum are required
- Lexin interview process
- And play the little chestnut of dynamic agent
- Mipi interface, DVP interface and CSI interface of camera
- 一文揭开,测试外包公司的真实情况
- Matrix and TMB package version issues in R
- 二分查找(折半查找)
- Target detection series - detailed explanation of the principle of fast r-cnn
猜你喜欢

第 2 章:小试牛刀,实现一个简单的Bean容器

【软件测试】06 -- 软件测试的基本流程

SD_ CMD_ RECEIVE_ SHIFT_ REGISTER

Hdu1232 unimpeded project (and collection)

611. 有效三角形的个数

Ros2 - Service Service (IX)

PHY drive commissioning - phy controller drive (II)

CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)

Ros2 - function package (VI)

docker安装mysql并使用navicat连接
随机推荐
Delayqueue usage and scenarios of delay queue
postmessage通信
PHY drive commissioning - phy controller drive (II)
HDU1231 最大连续子序列(分治or动规or双指针)
Ros2 - function package (VI)
The difference between new and malloc
能量守恒和打造能量缺口
Energy conservation and creating energy gap
Hdu1231 maximum continuous subsequence (divide and conquer or dynamic gauge or double pointer)
PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
(top) pretty girl binary color code portal
ImportError: No module named ‘Tkinter‘
SD_CMD_RECEIVE_SHIFT_REGISTER
arcgis_ spatialjoin
npm install -g/--save/--save-dev的区别
DataGrid offline installation of database driver
并发编程 — 如何中断/停止一个运行中的线程?
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
Special training of C language array
二分查找(折半查找)