selenium+python find_element_by_css_selector方法使用
1.通过类class获取
比如如下代码
<h1 class="important">
This heading is very important.
</h1>
<p class="important">
This paragraph is very important.
</p>
<p class="important warning">
This paragraph is a very important warning.
</p>
在上面的代码中,两个元素的 class 都指定为 important:第一个标题( h1 元素),第二个段落(p 元素)
1> 获取class值为important的h1标签
find_element_by_css_selector(h1.importane)
2>获取所有class值为important的标签
find_element_by_css_selector(*.importane)或者find_element_by_css_selector(.importane)
3>获取class值为important warning的标签
find_element_by_css_selector(.importane.warning)
2.通过id获取:
首先,ID 选择器前面有一个 # 号 - 也称为棋盘号或井号
<p id="intro">This is a paragraph of introduction.</p>
find_element_by_css_selector(#"intro")
3.属性选择器:
1>.
<a title="W3School Home" href="http://w3school.com.cn">W3School</a>
属性中包含了title和href,
find_element_by_css_selector('a[title][href]')
2>
<a href="http://www.w3school.com.cn/about_us.asp">About W3School</a>
定位属性中href="http://www.w3school.com.cn/about_us.asp"的元素,
find_element_by_css_selector('a[href="http://www.w3school.com.cn/about_us.asp"]')
3>
<a href="http://www.w3school.com.cn/" title="W3School">W3School</a>
通过href和title来定位
find_element_by_css_selector("a[href='http://www.w3school.com.cn/about_us.asp'][title='W3School']")
4>部分属性定位
<h1>可以应用样式:</h1>
<img title="Figure 1" src="/i/figure-1.gif" />
<img title="Figure 2" src="/i/figure-2.gif" /> <hr /> <h1>无法应用样式:</h1>
<img src="/i/figure-1.gif" />
<img src="/i/figure-2.gif" />
定位title中包含有figure的元素:
find_element_by_css_selector("image[title~='figure']")
5>其他
[abc^="def"] 选择 abc 属性值以 "def" 开头的所有元素
[abc$="def"] 选择 abc 属性值以 "def" 结尾的所有元素
[abc*="def"] 选择 abc 属性值中包含子串 "def" 的所有元素
4.后代选择器
<h1>This is a <em>important</em> heading</h1>
<p>This is a <em>important</em> paragraph.</p>
find_element_by_css_selector("h1 em")
selenium+python find_element_by_css_selector方法使用的更多相关文章
- Selenium+Python常见定位方法
参见官网:http://selenium-python.readthedocs.io/locating-elements.html 有多种策略来定位页面中的元素.你可以使用最适合你的情况.Seleni ...
- selenium+python自动化测试
F12: 右键 选择复制 path 在selenium+python自动化测试(一)–环境搭建中,运行了一个测试脚本,脚本内容如下: from selenium import webdriver ...
- selenium + python自动化测试unittest框架学习(五)webdriver的二次封装
因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<se ...
- selenium+python+unittest实现自动化测试(入门篇)
本文主要讲解关于selenium自动化测试框架的入门知识点,教大家如何搭建selenium自动化测试环境,如何用selenium+python+unittest实现web页面的自动化测试,先来看看se ...
- selenium + python 自动化测试环境搭建
selenium + python 自动化测试 —— 环境搭建 关于 selenium Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操 ...
- <译>Selenium Python Bindings 5 - Waits
如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...
- <译>Selenium Python Bindings 2 - Getting Started
Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...
- <译>Selenium Python Bindings 1 - Installation
Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...
- selenium python 第一个脚本
为什么选择python?我的回答很简单,简单易学,功能强大! 下面看看python和selenium 2的结合是什么样子吧 一.第一个selenium python脚本: #coding = utf- ...
随机推荐
- 基于http协议的api接口对于客户端的身份认证方式以及安全措施
由于http是无状态的,所以正常情况下在浏览器浏览网页,服务器都是通过访问者的cookie(cookie中存储的jsessionid)来辨别客户端的身份的,当客户端进行登录服务器也会将登录信息存放在服 ...
- 【Android 界面效果35】管理Fragments
http://www.cnblogs.com/mengdd/archive/2013/01/09/2853254.html
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【MYSQL】数据类型
转载 https://www.baidu.com/s?ie=UTF-8&wd=cnblog 原文 泪云山海的博客 mysql 数据类型 1.整型 MySQL数据类型 含义(有符号) tinyi ...
- Precompile Prefix file(.pch文件)
参考资料: http://blog.csdn.net/lwjok2007/article/details/46385595 http://www.tuicool.com/articles/beURbe ...
- 【Amazon Linux】免费搭建subversion服务器
Amazon的EC2服务器可以免费试用一年.在这里申请: https://aws.amazon.com/cn/free/ 尝试把它弄成一个svn库来保存代码.按照 http://northwaygam ...
- poj 1947 树形dp
思路:dp[i][j]表示,以i节点为根,删去j个节点最少要断几条边. 那么dp[u][j]=min(dp[u][j],dp[v][k]+dp[u][j-k]);//选取最优状态 dp[u][j]=m ...
- Pascal 语言中约瑟夫问题:幸运观众
[题目]节目主持人准备从n名学生中挑选一名幸运观众,因为大家都想争当幸运观众,老师只好采取这样的办法:全体同学站成一列,由前面往后面依顺序报数.1,2,1,2……报单数的同学退出队伍,剩下的同学向前靠 ...
- 【.NET基础】--委托、事件、线程(2)
本文介绍event的使用以及原理,本文接上一篇文章的Demo继续[下载上一篇Demo] 上一篇我们在类(dg_SayHi.cs)里面定义代理了4个Delegate,然后在Button的后台事件中 新建 ...
- JDBC学习笔记(一)
public static void main(String[] args) { ResultSet rs = null; Statement stmt = null; Connection conn ...