为了这口醋,包的这饺子。为了Selenium,学有限的CSS,逐步替换XPATH

Learn about CSS rules and pseudo-classes to help you move your XPATH locators to CSS.

  • 1. 最基本
        • Id
        • Element Type
        • Direct Child
        • Child or Sub-Child
        • Class
  • 2. 深入一点
      • Next Sibling
        • Attribute Values
        • Choosing a Specific Match
      • Sub-String Matches
  • 3 参考资料

In order for Selenium to click on an element, type into it, or mouse in or out, the tool first needs to find the element.
The WebDriver code library provides methods to do just that, such as

  • findelement()
  • findelements().

These usually take a locator, which can be created by ID, XPATH Code, or Cascading Style Sheets (CSS). Getting the XPATH code can be as easy as selecting the element in developer tools or using something like Chropath.

XPath简单,但不可靠。任何软件升级都有可能造成Path的变化。更不用说反扒技术的存在了。
ID最好,但不常有。
颠来倒去,还是CSS靠谱些。但学起来费劲。

不过,为了Selenium的目的,CSS还是有限费劲,可整。

A CSS Selector is a combination of an element selector and a value which identifies the web element within a web page. They are string representations of HTML tags, attributes, Id and Class. As such they are patterns that match against elements in a tree and are one of several technologies that can be used to select nodes in an XML document.

1. 最基本

Id

An element’s id in XPATH is defined using: [@id='example']and in CSS using: # - ID’s must be unique within the DOM.

XPath: //div[@id='exampleid']
CSS: #exampleid
Element Type

The previous example showed //div in the xpath. That is the element type, which could be input for a text box or button, img for an image, or a for a link.

Xpath: //input or
Css: =input
Direct Child

HTML pages are structured like XML, with children nested inside of parents. If you can locate, for example, the first link within a div, you can construct a string to reach it. A direct child in XPATH is defined by the use of a /, while on CSS, it’s defined using >.

XPath: //div/a
CSS: div > a
Child or Sub-Child

Writing nested divs can get tiring - and result in code that is brittle. Sometimes you expect the code to change, or want to skip layers. If an element could be inside another or one of its children, it’s defined in XPATH using // and in CSS just by a whitespace.

XPath: //div//a
CSS: div a
Class

For classes, things are pretty similar in XPATH: [@class='example'] while in CSS it’s just .


XPath: //div[@class='example']
CSS: .example

2. 深入一点

Next Sibling

This is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that’s inside the same parent. Let’s show an example using a form to select the field after username.

<form class = "form-signin" role = "form" action = "/index.php" method = "post">
<h4 class = "form-signin-heading"></h4> 
<input type = "text" class = "form-control" id = "username" name = "username" placeholder = "username" required autofocus></br> 
<input type = "password" class = "form-control" id = "password" name = "password" placeholder = "password" required> 
<p> 
<button class = "btn btn-lg btn-primary btn-block radius" type = "submit" name = "login">Login</button> 
</form> 

Let’s write an XPath and css selector that will choose the input field after “username”. This will select the “alias” input, or will select a different element if the form is reordered.

XPATH: //input[@id='username']/following-sibling:input[1]
CSS: #username + input
Attribute Values

If you don’t care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value. A good example would be choosing the ‘username’ element of the form above without adding a class.

We can easily select the username element without adding a class or an id to the element.

XPATH: //input[@name='username']
CSS: input[name='username']

We can even chain filters to be more specific with our selectors.

XPATH: //input[@name='login'and @type='submit']
CSS: input[name='login'][type='submit']

Here Selenium will act on the input field with name=“login” and type=“submit”

Choosing a Specific Match

CSS selectors in Selenium allow us to navigate lists with more finesse than the above methods. If we have a ul and we want to select its fourth li element without regard to any other elements, we should use nth-child or nth-of-type. Nth-child is a pseudo-class. In straight CSS, that allows you to override behavior of certain elements; we can also use it to select those elements.

<ul id = "recordlist"><li>Cat</li><li>Dog</li><li>Car</li><li>Goat</li>
</ul>

If we want to select the fourth li element (Goat) in this list, we can use the nth-of-type, which will find the fourth li in the list. Notice the two colons, a recent change to how CSS identifies pseudo-classes.

CSS: #recordlist li::nth-of-type(4)

On the other hand, if we want to get the fourth element only if it is a li element, we can use a filtered nth-child which will select (Car) in this case.

CSS: #recordlist li::nth-child(4)

Note, if you don’t specify a child type for nth-child it will allow you to select the fourth child without regard to type. This may be useful in testing css layout in selenium.

CSS: #recordlist *::nth-child(4)

In XPATH this would be similar to using [4].

Sub-String Matches

CSS in Selenium has an interesting feature of allowing partial string matches using ^=, $=, or *=. I’ll define them, then show an example of each:

^= Match a prefix
CSS: a[id^='id_prefix_']A link with an “id” that starts with the text “id_prefix_”$= Match a suffix
CSS: a[id$='_id_sufix']A link with an “id” that ends with the text “_id_sufix”*= Match a substring
CSS: a[id*='id_pattern']A link with an “id” that contains the text “id_pattern”

在这里插入图片描述

3 参考资料

  1. Locating Elements
  2. SauceLabs

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/621076.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

transfomer中Multi-Head Attention的源码实现

简介 Multi-Head Attention是一种注意力机制,是transfomer的核心机制. Multi-Head Attention的原理是通过将模型分为多个头&#xff0c;形成多个子空间&#xff0c;让模型关注不同方面的信息。每个头独立进行注意力运算&#xff0c;得到一个注意力权重矩阵。输出的结果再通过…

SVN切换账户

前言&#xff08;svn切换&#xff09; 本文章简单写下SVN账户切换操作 linux 1.删除目录 ~/.subversion/auth/ 下的所有文件。 2.再次操作svn时可重新输入用户名和密码。 windows (1)在工程中单击右键,单击"TortoiseSVN"。 (2)选择"Setting"。 (3)选择&quo…

C语言实现快排核心思想(双指针法)

核心代码&#xff1a; 这就是每一趟快排的实现代码&#xff0c;由上面的动图&#xff0c;我们能知道前后指针法的核心是玩好cur和prev这两个指针&#xff0c;具体的逻辑是cur找比key小的值&#xff0c;找到就prev&#xff0c;然后prev和cur的值就进行交换&#xff0c;但是总不能…

统信UOS操作系统上禁用IPv6

原文链接&#xff1a;统信UOS操作系统上禁用IPv6 hello&#xff0c;大家好啊&#xff01;继之前我们讨论了如何在麒麟KYLINOS上禁用IPv6之后&#xff0c;今天我要给大家带来的是在统信UOS操作系统上禁用IPv6的方法。IPv6是最新的网络通信协议&#xff0c;但在某些特定的网络环境…

PiflowX-DorisWrite组件

DorisWrite组件 组件说明 往Doris存储写入数据。 计算引擎 flink 组件分组 doris 端口 Inport&#xff1a;默认端口 outport&#xff1a;默认端口 组件属性 名称展示名称默认值允许值是否必填描述例子fenodesFenodes“”无是Doris FE http地址&#xff0c; 支持多个…

基于企业级SaaS低代码平台的协同制造产品解决方案

万界星空科技低代码平台提供的MES&#xff0c;WMS&#xff0c;QMS等应用&#xff0c;是助力企业从数字化工厂向数字化企业升级的落地管道及载体&#xff0c;能帮助企业在数字化转型的过程中&#xff0c;实现制造企业与其供应链的协同制造。从订单发出、供应商确认、供应商生产、…

使用setdefault撰写文本索引脚本(出自Fluent Python案例)

背景介绍 由于我们主要介绍撰写脚本的方法&#xff0c;所以用一个简单的文本例子进行分析 a[(19,18),(20,53)] Although[(11,1),(16,1),(18,1)] ambiguity[(14,16)] 以上内容可以保存在一个txt文件中&#xff0c;任务是统计文件中每一个词&#xff08;包括字母&#xff0c;数…

评估LLM在细胞数据上的实用性(2)-细胞层面的评估

本文衔接上一篇&#xff1a;评估LLM在细胞数据上的实用性(1)-基本概述 目录 定义参数和任务批次整合多模态整合细胞类型注释 细胞层面的评估批次整合多模态整合细胞类型注释 定义 我们考虑一个预训练LLM表示为 M ( x , θ ) M(x,\theta) M(x,θ)&#xff0c;其基于单细胞数据…

RAG 评估框架 -- RAGAS

原文 引入 RAG&#xff08;Retrieval Augmented Generation&#xff09;的原因 随着ChatGPT的推出&#xff0c;很多人都理所当然直接用LLM当作知识库回答问题。这种想法有两个明显的缺点&#xff1a; LLM无法得知在训练之后所发生的事情&#xff0c;因此无法回答相关的问题存…

从零开始学习Python基础语法:打开编程大门的钥匙

文章目录 一、引言1.1 Python作为一种流行的编程语言的介绍1.2 Python的应用领域和适用性 二、为什么选择Python2.1 Python的简洁性和易读性2.2 Python的跨平台特性 三、Python在数据科学和人工智能领域的应用3.1 第一个Python程序3.1.1 Hello, World!&#xff1a;编写并运行你…

统信UOS_麒麟KYLINOS上使用Remmina远程Windows并传输文件

原文链接&#xff1a;统信UOS/麒麟KYLINOS上使用Remmina远程Windows并传输文件 hello&#xff0c;大家好啊&#xff01;继之前我们讨论了在统信UOS/麒麟KYLINOS与Windows之间通过Open SSH实现文件传输之后&#xff0c;今天我要给大家带来的是如何使用Remmina软件在统信UOS/麒麟…

12.2内核空间基于SPI总线的OLED驱动

在内核空间编写SPI设备驱动的要点 在SPI总线控制器的设备树节点下增加SPI设备的设备树节点&#xff0c;节点中必须包含 reg 属性、 compatible 属性、 spi-max-frequency 属性&#xff0c; reg 属性用于描述片选索引&#xff0c; compatible属性用于设备和驱动的匹配&#xff…

【数据结构】树和二叉树堆(基本概念介绍)

&#x1f308;个人主页&#xff1a;秦jh__https://blog.csdn.net/qinjh_?spm1010.2135.3001.5343&#x1f525; 系列专栏&#xff1a;《数据结构》https://blog.csdn.net/qinjh_/category_12536791.html?spm1001.2014.3001.5482 ​​ 目录 前言 树的概念 树的常见名词 树与…

2024.1.14每日一题

LeetCode 83.删除排序链表中的重复元素 83. 删除排序链表中的重复元素 - 力扣&#xff08;LeetCode&#xff09; 题目描述 给定一个已排序的链表的头 head &#xff0c; 删除所有重复的元素&#xff0c;使每个元素只出现一次 。返回 已排序的链表 。 示例 1&#xff1a; 输…

关闭免费版pycharm社区版双击shift时出现的搜索框

Pycharm 在双击 shift 的时候总是弹出搜索框&#xff0c;但作为中国玩家&#xff0c;经常需要双击 shift 循环切换中英文。这就很困恼。 下面就解决这个问题。单独关闭双击shift的功能。 步骤 1.左上角 File -> Settings 2. 如图&#xff0c;输入‘advan’ 找到高级设置&…

RibbonGroup 添加QRadioButton

RibbonGroup添加QRadioButton&#xff1a; QRadioButton * pRadio new QRadioButton(tr("Radio")); pRadio->setToolTip(tr("Radio")); groupClipboard->addWidget(pRadio); connect(pRadio, SIGNAL(clicked(…

扩展卡尔曼滤波(Extended Kalman Filter, EKF):理论和应用

扩展卡尔曼滤波&#xff08;Extended Kalman Filter, EKF&#xff09;&#xff1a;理论、公式和应用 引言 卡尔曼滤波是一种广泛应用于估计动态系统状态的技术&#xff0c;但当系统的动态模型或测量模型是非线性的时候&#xff0c;传统的卡尔曼滤波方法就显得无能为力。扩展卡…

【保姆级教程|YOLOv8添加注意力机制】【1】添加SEAttention注意力机制步骤详解、训练及推理使用

《博主简介》 小伙伴们好&#xff0c;我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。 ✌更多学习资源&#xff0c;可关注公-仲-hao:【阿旭算法与机器学习】&#xff0c;共同学习交流~ &#x1f44d;感谢小伙伴们点赞、关注&#xff01; 《------往期经典推…

SpringBoot+thymeleaf实战遇到的问题

目录 一、控制台&#xff1a; 二、数据库查询异常&#xff1a; 三、前后端错误校验 四、在serviceImp中需要添加一个eq条件&#xff0c;表示和数据库中的哪个字段进行比较&#xff0c;否则会查出所有数据&#xff0c;导致500 五、使用流转换数据更简洁 六、重复报错&…

动态规划篇-03:打家劫舍

198、打家劫舍 状态转移方程 base case 边界问题就是&#xff1a;走到最后一间房子门口也没抢&#xff0c;那么最终抢到的金额为0 明确状态 “原问题和子问题中会变化的变量” 抢到的金额数就是状态&#xff0c;因为随着在每一件房子门口做选择&#xff0c;抢到的金额数会随…