文章目录

  • 前言
  • 源码及使用前准备
  • 使用方法
    • above 以上
    • below 以下
    • to_left_of 往左
    • to_right_of 往右
    • near 附近
    • 组合操作 查看右下方所有元素
    • with_tag_name和locate_with的区别
  • 结语

前言

selenium4中新增了相对定位器  以下为官方介绍:

这里以python做演示,用其他语言的小伙伴可以看看官方文档
点我跳转到官方文档哟


`Relative Locators`
Selenium 4 introduces Relative Locators (previously called as Friendly Locators). These locators are helpful when it is not easy to construct a locator for the desired element, but easy to describe spatially where the element is in relation to an element that does have an easily constructed locator.How it works
Selenium uses the JavaScript function getBoundingClientRect() to determine the size and position of elements on the page, and can use this information to locate neighboring elements.
find the relative elements.Relative locator methods can take as the argument for the point of origin, either a previously located element reference, or another locator. In these examples we’ll be using locators only, but you could swap the locator in the final method with an element object and it will work the same."顾名思义: 相对定位器就是根据某个元素去定位其他元素,这里新增了5个方法"
"分别为: 以上 above, 往下 below, 往左 to_left_of,往右 to_right_of, 附近 near"
"这些方法也可以组合使用 有时元素最容易被识别为在一个元素的上方/下方和另一个元素的右/左。"
submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"})

源码及使用前准备

源码如图包内包含两个方法 with_tag_name、locate_with及一个类RelativeBy
因为两个方法的返回都是调用的RelativeBy类 所以直接调用两个方法即可

所以需要先导包

from selenium.webdriver.support.relative_locator import locate_with, with_tag_name

使用方法

这里使用鹅厂的门户网站演示 界面如下图

above 以上

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with, with_tag_namedri = webdriver.Chrome()
dri.get('https://qq.com')# 首先定位qq空间
dri.find_element(By.ID, 'm-product')
q_zone = dri.find_element(By.PARTIAL_LINK_TEXT,'空间')
print(q_zone.text) #验证一下# 定位上面的 体育APP
q_sports = locate_with(By.TAG_NAME, 'a').above(q_zone)   #  这里分两步写是为个看着直观一点
print(dri.find_element(q_sports).text) # 验证是不是定位到 体育APPdri.quit()

结果如图所示

below 以下

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with, with_tag_namedri = webdriver.Chrome()
dri.get('https://qq.com')# 首先定位qq空间
dri.find_element(By.ID, 'm-product')
q_zone = dri.find_element(By.PARTIAL_LINK_TEXT,'空间')
print(q_zone.text) #验证一下# 定位下面的 CF
q_cf = locate_with(By.TAG_NAME, 'a').below(q_zone)  #  这里分两步写是为个看着直观一点
print(dri.find_element(q_cf).text) # 验证是不是定位到 CFdri.quit()

结果如图所示

to_left_of 往左

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with, with_tag_namedri = webdriver.Chrome()
dri.get('https://qq.com')# 首先定位qq空间
dri.find_element(By.ID, 'm-product')
q_zone = dri.find_element(By.PARTIAL_LINK_TEXT,'空间')
print(q_zone.text) #验证一下# 定位下面的 QQ
q_qq = locate_with(By.TAG_NAME, 'a').to_left_of(q_zone)  #  这里分两步写是为个看着直观一点
print(dri.find_element(q_qq).text) # 验证是不是定位到 QQdri.quit()

结果如图所示

to_right_of 往右

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with, with_tag_namedri = webdriver.Chrome()
dri.get('https://qq.com')# 首先定位qq空间
dri.find_element(By.ID, 'm-product')
q_zone = dri.find_element(By.PARTIAL_LINK_TEXT,'空间')
print(q_zone.text) #验证一下# 定位右边的企业微信
q_work = locate_with(By.TAG_NAME, 'a').to_right_of(q_zone)  #  这里分两步写是为个看着直观一点
print(dri.find_element(q_work).text) # 验证是不是定位到 企业微信dri.quit()

结果如图所示

near 附近

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with, with_tag_namedri = webdriver.Chrome()
dri.get('https://qq.com')# 首先定位qq空间
dri.find_element(By.ID, 'm-product')
q_zone = dri.find_element(By.PARTIAL_LINK_TEXT,'空间')
# print(q_zone.text) #验证一下# 定位附近标签
q_work = locate_with(By.TAG_NAME, 'a').near(q_zone)  #  这里分两步写是为个看着直观一点
els = dri.find_elements(q_work)for i in els:print(i.text)dri.quit()

结果如图所示 这里我用find_elements把附近所有的元素都找出来 这里跑了两次 元素的顺序是一样的

组合操作 查看右下方所有元素

原图

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with, with_tag_namedri = webdriver.Chrome()
dri.get('https://qq.com')# 首先定位qq空间
dri.find_element(By.ID, 'm-product')
q_zone = dri.find_element(By.PARTIAL_LINK_TEXT,'空间')
print(q_zone.text) #验证一下# 定位CF
q_cf = dri.find_element(locate_with(By.TAG_NAME, 'a').below(q_zone))# 定位右下方
q_work = locate_with(By.TAG_NAME, 'a').to_right_of(q_cf).below(q_zone)  #  这里分两步写是为个看着直观一点
els = dri.find_elements(q_work)for i in els:print(i.text)dri.quit()

结果 这里定位到了网页最下面 且后面两行只定位了2个标签 至于为什么 如果有兴趣的同学可以自己研究一下

with_tag_name和locate_with的区别

先看源代码

def with_tag_name(tag_name: str) -> "RelativeBy":"""Start searching for relative objects using a tag name.Note: This method may be removed in future versions, please use`locate_with` instead.:Args:- tag_name: the DOM tag of element to start searching.:Returns:- RelativeBy - use this object to create filters within a`find_elements` call."""if not tag_name:raise WebDriverException("tag_name can not be null")return RelativeBy({"css selector": tag_name})def locate_with(by: By, using: str) -> "RelativeBy":"""Start searching for relative objects your search criteria with By.:Args:- by: The value from `By` passed in.- using: search term to find the element with.:Returns:- RelativeBy - use this object to create filters within a`find_elements` call."""assert by is not None, "Please pass in a by argument"assert using is not None, "Please pass in a using argument"return RelativeBy({by: using})

这里的源码还是挺简单的 显而易见

with_tag_name()  # 括号内直接写标签名
locate_with() # 和 find_element()写法一样 用到by 和 元素定位表达式

结语

文中代码部分 有些注释里的方位没有改 写完后才发现 因为这对文章内容没啥影响 这里就不改了
因为appium2是依赖selenium4的 所以个人觉得相对定位对于app自动化应该很有用 当然这只是个人猜测 至于能不能在app自动化中使用,有兴趣的小伙伴可以自行测试 用法应该都是一样的,就不再发文了

浅谈selenium4新增功能之相对定位相关推荐

  1. 校园计算机网的功能,浅谈校园网的功能、建设与管理

    浅谈校园网的功能.建设与管理 浅谈校园网的功能.建设与管理 三明市第九中学 林孜成 摘要: 随着Internet大潮的到来,各所中小学也纷纷建立了校园网并接入Internet,但在建设和管理过程中,由 ...

  2. IC设计- 浅谈各种验证 - 功能验证,形式验证,原型验证

    浅谈逻辑仿真,形式验证及硬件仿真 随着硬件设计复杂性的不断增加,为了能够最大程度的使得验证收敛,验证方法也越来越多,今天我们针对常见的几种验证方法做一些简单的分析,指出它们的常用应用环境以及一些优缺点 ...

  3. 浅谈游戏服务器---功能模块上来看

    游戏服务器在网游上的作用不容考虑,游戏能做大到什么程度,还是有很大的依靠的,这篇文章先从功能模块的角度来谈一个完善的游戏服务器需要实现哪.     一:游戏服务器的作用:连接各个网游客户端,实现各客户 ...

  4. 浅谈Inventor2023-实用功能大揭秘~

    熟悉CAD三维的朋友都知道,它的最佳拍档Inventor. Inventor是一款三维可视化实体模拟软件,提供了专业级机械设计.文档编制和产品仿真工具.最新版本的 Inventor® 三维 CAD 软 ...

  5. 浅谈权限(功能权限数据权限)

    一般企业上的权限部分,都是区分为功能权限和数据权限. 一.功能权限 功能权限,就是用户登录后,能看到哪些菜单,能看到哪些按钮,能执行哪些操作的权限. 一般,功能权限,已经都有很成熟的业内方案和框架了. ...

  6. 浅谈商学院的功能以及重要性

    1.图文课程:代理前段商学院里面平台规格的内容,可以上传文字.图片.视频连接 2.视频课程:可以上传视频,代理可以直接在线观看视频 3.可以进行直播可以上传直播连接,代理可以直接在线观看直播平台:荔枝 ...

  7. 移动支付被重定义,浅谈iPhone6的NFC功能

    原标题 重定义移动支付 浅谈iPhone6 NFC功能 每一代iphone的上市都会迎来无数拥趸的追捧,这是乔布斯用生命打造的品牌口碑,苹果就是手机行业最时尚,最高端的象征.这次iphone6同样如 ...

  8. Exchange server 2010系列教程之五 浅谈Outlook 2010新功能

    Exchange server 2010系列教程之五  浅谈Outlook 2010新功能 前面我们讲了outlook的配置事项,也说了exchange server 2010的正确卸载方法.下面简要 ...

  9. java dao service实例_浅谈Action+Service +Dao 功能

    1. Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. D ...

最新文章

  1. ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法
  2. 【转载】FckEditor 2.6.3 for Java 2.4 配置
  3. 云上的精准医疗——公有云、私有云案例分析和比较
  4. audio 上一首 下一首 自定义样式_HTML5中 audio标签的样式修改
  5. 笔记-高项案例题-2016年上-计算题
  6. 计算机网络何顶新pdf,《计算机网络及应用(何顶新)(二版)》【摘要 书评 在线阅读】-苏宁易购图书...
  7. 与或非逻辑 页面展示html,一种实现与非、或非门逻辑的忆阻器电路
  8. .Net WebApi接口之Swagger集成详解
  9. 20岁生日快乐c语言,C语言怎样编程生日快乐代码
  10. R语言将两个矩阵数据进行相乘
  11. 京东联盟新版API接口PHP版SDK的坑
  12. ssm水电费管理系统java
  13. ARP命令详解和解决ARP攻击
  14. ibd 导入mysql_mysql导入frm和ibd文件还原数据
  15. 边境的悍匪—机器学习实战:第十二章 使用TensorFlow自定义模型和训练
  16. html给图片绝对定位,html相对定位和绝对定位
  17. WAV文件格式全面分析+使用CoolEdit生成正弦波
  18. stitching.cpp鱼眼图像拼接融合 源码分析
  19. 18天掌握Java SE jvav梳理总结 从jvav到架构师
  20. 如果你的网站需要免费的 SVG 插图,一定不要错过 Undraw 这个网站

热门文章

  1. Collection 接口(含迭代器)
  2. ios 打印 详细错误日志_ios 打印日志注意的点
  3. 回放video 加 自定义tabBar 进度条
  4. Chrome浏览器渲染原理笔记
  5. 金融大数据解决方案:如何做金融行业的数据治理?
  6. 版本控制服务器-git
  7. (Git)了解Git(分布式版本控制系统)和安装Git步骤
  8. 职称 计算机免试理由,职称计算机考试免试申请表.doc
  9. ssm 框架上传图片到服务器
  10. 一些日常必备的社工网址与查询