This page will show you some CSS rules and pseudo-classes that will help you move your XPATH locators to CSS, a native approach on all browsers.

I: Simple

Direct child

A direct child in XPATH is defined by the use of a “/“, while on CSS, it’s defined using “>”

Examples:

//div/acss=div > a

Child or subchild

If an element could be inside another or one it’s childs, it’s defined in XPATH using “//” and in CSS just by a whitespace.

Examples:

//div//acss=div a

Id

An element’s id in XPATH is defined using: “[@id='example']” and in CSS using: “#”

Examples:

//div[@id='example']//acss=div#example a

Class

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

Examples:

//div[@class='example']//acss=div.example a

II: Advanced

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.

</input> </input>

Let’s write a 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.

css=form input.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 without adding a class.

</input> </input> </input> </input>

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

css=form input[name='username']

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

css=input[name='continue'][type='button']

Here Selenium will act on the input field with name=”continue” and type=”button”

Choosing a specific match

CSS selectors in Selenium allow us to navigate lists with more finess that 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.

  • <p>Heading</p>
  • Cat
  • Dog
  • Car
  • Goat

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.

css=ul#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=ul#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=ul#recordlist *:nth-child(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”

Matching by inner text

And last, one of the more useful pseudo-classes, :contains() will match elements with the desired text block:

css=a:contains('Log Out')

This will find the log out button on your page no matter where it’s located. This is by far my favorite CSS selector and I find it greatly simplifies a lot of my test code.

转载于:https://www.cnblogs.com/xxsl/p/6116408.html

转:SELENIUM TIPS: CSS SELECTORS相关推荐

  1. Selenium Tips - CSS定位元素

    Selenium Tips - CSS定位元素 原文: http://sauceio.com/index.php/2010/01/selenium-totw-css-selectors-in-sele ...

  2. selenium CSS Selectors基础用法

    I: Simple Direct child A direct child in XPATH is defined by the use of a "/", while on CS ...

  3. 获取css selector,selenium的css selector元素获取方式

    目标站存在验证码,验证码与用户名关联:登录密码存在加密,加密方式未知(相同用户密码反复测试加密结果不一样):无法使用burp+验证码识别爆破:只能使用selenuim+验证码识别处理: 测试中发现几个 ...

  4. Selenium使用CSS定位 总结

    大部分人在使用selenium定位元素时,用的是xpath定位,css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁 css的定位方法和上一篇的xpath定位方法,两篇学习 ...

  5. css选择器(css Selectors)的语法分析

    css选择器分为以下几类: 1.元素选择符 Element Selectors 2.关系选择符 Relationship Selectors 3.属性选择符 Attribute Selectors 4 ...

  6. The 30 CSS Selectors you Must Memorize

    为什么80%的码农都做不了架构师?>>>    1. * * { margin: 0; padding: 0; } Let's knock the obvious ones out, ...

  7. selenium学习----CSS selector

    CSS Selector选择web页面的元素 CSS是用来对web页面进行渲染的,因此必然涉及到web页面上某一元素的选择,比如页面某些内容显示效果,可以讲 CSS Selector 语法 天生就是浏 ...

  8. css定位和xpath定位,selenium的css定位与xpath定位

    大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁. 一:css:属性 ...

  9. selenium之css定位详解

    css定位 css概述: 1.css(Cascading Style Sheets)是一种语言,它用来描述HTML和XML的元素显示样式. 2.css语言中有css选择器,在selenium中也可以使 ...

最新文章

  1. 顺序结构,判断结构 if,switch
  2. 八数码(康拓展开标记)及类似题
  3. EXE4J(Windows Launcher Generator)——EXE4J概述、下载以及使用
  4. JavaWeb课程复习资料(四)——创建Servlet
  5. Wireshark个人实战总结
  6. Nvidia Jetson TX2+Intel Realsense D435i跑ORB_SLAM3
  7. 在CentOS7上安装RocketMQ 4.8.0
  8. OpenCV搜索文件夹中的图片并保存图片路径和信息
  9. java day09【继承、super、this、抽象类】
  10. 【渝粤教育】国家开放大学2018年秋季 0221-22T数字电子电路 参考试题
  11. Windows Server 2016-DHCP服务器审核日志大小调整
  12. 论文Chinese Relation Extraction with Multi-Grained Information and External Linguistic Knowledge阅读笔记
  13. LU分解 LDL分解 Cholesky分解
  14. 百度云下载文件方法:PC 和 群晖 Docker
  15. 漫画|望京和西二旗全解读:最美互联网人
  16. Element组件框架
  17. js-url转换blob以及blob与base64的相互转换
  18. androidjni开发!应聘高级Android工程师历程感言,快来收藏!
  19. NumPy与ndarray简介
  20. Python super( ) 函数详解

热门文章

  1. 2011:吹响三网融合集结号
  2. 基于FME实现CAD图的三维多段线批量转多段线工具,dwg三维多段线批量转多段线工具
  3. 基于STM32的Flash读写详解
  4. CSR8670项目实战:BlueDongle 蓝牙音频测试dongle
  5. 商业客流统计系统商场实际应用功能
  6. 标签右浮动顺序倒转问题
  7. TensorFlow 2.0 教程25:Transformer
  8. 卫星信号处理跟踪流程
  9. 4G/5G工业路由器视频传输应用方案
  10. 手机端 自定义简单的下拉动画效果选择框 解决过渡失效