转:SELENIUM TIPS: CSS SELECTORS
I: Simple
Direct child
A direct child in XPATH is defined by the use of a “/“, while on CSS, it’s defined using “>”
Examples:
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:
Id
An element’s id in XPATH is defined using: “[@id='example']”
and in CSS using: “#”
Examples:
Class
For class, things are pretty similar in XPATH: “[@class='example']”
while in CSS it’s just “.”
Examples:
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.
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.
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.
We can easily select the username element without adding a class or an id to the element.
We can even chain filters to be more specific with our selections.
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.
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.
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.
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
A link with an “id” that starts with the text “id_prefix_”
$= Match a suffix
A link with an “id” that ends with the text “_id_sufix”
*= Match a substring
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:
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.
转:SELENIUM TIPS: CSS SELECTORS的更多相关文章
- css selectors tips
from https://saucelabs.com/resources/articles/selenium-tips-css-selectorsSauce Labs uses cookies to ...
- CSS selectors for Selenium with example,selenium IDE
CSS selectors for Selenium with example http://seleniumeasy.com/selenium-tutorials/css-selectors-tut ...
- [Selenium] 在Chrome的开发者工具中验证检查XPath/CSS selectors
Evaluate and validate XPath/CSS selectors in Chrome Developer Tools Method 1 : From Elements panel U ...
- 自动化测试-6.selenium的css定位
前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...
- BeautifulSoup高级应用 之 CSS selectors /CSS 选择器
BeautifulSoup支持最常用的CSS selectors,这是将字符串转化为Tag对象或者BeautifulSoup自身的.select()方法. 本篇所使用的html为: html_doc ...
- (3)选择元素——(4)css选择器(CSS selectors)
The jQuery library supports nearly all of the selectors included in CSS specifications 1 through 3, ...
- CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element name, id ...
- Why are dashes preferred for CSS selectors / HTML attributes?
Why are dashes preferred for CSS selectors / HTML attributes? I use dashes because I don't have to h ...
- flight.Archives001 / CSS Selectors选择器
Title/CSS选择器 序 : 这是flight.Archives 梦开始的地方, 作者我熬夜肝出来了这篇文章... 保证这是最简洁高效的 CSS Selectors 教程 Note : 暂时没有能 ...
随机推荐
- nginx 反向代理 取得真实IP和域名
nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递 ...
- epoll 简单介绍及例子
第一部分:Epoll简介 . 当select()返回时,timeout参数的状态在不同的系统中是未定义的,因此每次调用select()之前必须重新初始化timeout和文件描述符set.实际上,秒,然 ...
- jbpm node signal
task-node (任务节点) 其性质和node节点一样,在没有task的时候,也都是自动执行,不等待.task-node被归类为一个等待节点,是指在task-node中的task列表中的task没 ...
- ubuntu如何开启root,如何启用Ubuntu中root帐号
jingyan.baidu.com/article/495ba84116104238b20ede62.html ubuntu如何开启root,如何启用Ubuntu中root帐号 | 浏览:8344 | ...
- 【nodejs】使用Node.js实现REST Client调用REST API
最近在产品中开发基于REST的API接口,结合自己最近对Node.js的研究,想基于它开发一个REST Client做测试之用. 通过初步研究,Node.js开发HTTP Client还是挺方便的. ...
- FW nexus docker
原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy) ...
- Qt获取组合键
CTRL+Enter发送信息的实现 在现在的即时聊天程序中,一般都设置有快捷键来实现一些常用的功能,类似QQ可以用CTRL+Enter来实现信息的发送. 在QT4中,所有的事件都继承与QEvent这个 ...
- String.format(转)
转自:http://blog.csdn.net/lonely_fireworks/article/details/7962171 方便自己查阅. 常规类型的格式化 String类的format()方法 ...
- Java学习-029-JSON 之三 -- 模仿 cssSelector 封装读取 JSON 数据方法
前文简单介绍了如何通过 json-20141113.jar 提供的功能获取 JSON 的数据,敬请参阅:Java学习-028-JSON 之二 -- 数据读取. 了解学习过 JQuery 的朋友都知道, ...
- css 清除浮动(转)
转自http://hi.baidu.com/kongcheng2012/item/2b1250d4452e802538f6f705 为什么浮动这么难? 因为浮动会使当前标签产生向上浮的效果,同时会影响 ...