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- ...
随机推荐
- Hadoop学习笔记(1) ——菜鸟入门
Hadoop学习笔记(1) ——菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序. ...
- Winform---文件夹操作
一.文件夹的操作 private void button1_Click(object sender, EventArgs e) { //文件夹操作 ////新建文件夹 //Directory.Crea ...
- spring三大核心学习(一)---控制反转
记得当年大学时候,java的企业级框架还是ssh的天下(spring,struts和hibernate),但是现在,感觉spring已经完全把那两个框架甩在后边了.用spring的人越来越多,用str ...
- vim 高级使用技巧
前言:逃离windows有很长时间了,特别是当今android盛行的时代,我们没有理由不选择ubuntu作为编译开发android之首选.其实操作系统只是我们使用的一个工具, windows也好lin ...
- Iframe跨域_ASP.NET
1.描述: A系统 需要 调用 B系统的页面,被调用的B系统的页面b.html内部嵌套了iframe框架c.aspx地址页 2.问题呈现: ie浏览器下 Chrome浏览器下 追踪 3.问题原因: X ...
- UI1_UITableViewHomeWork
// // AppDelegate.m // UI1_UITableViewHomeWork // // Created by zhangxueming on 15/7/14. // Copyrigh ...
- HTTP状态码参考
1. HTTP状态码意义 客户机与服务器建立连接后,发送一个请求给服务器(如:Get /index.html http/1.1),在服务器接到请求后,给予客户机相应的响应信息,包括该信息的协议版本号. ...
- javascript之面向对象程序设计(对象和继承)
总结的文章略长,慎点. 知识点预热 引用类型:引用类型的值(对象)是引用类型的一个实例.在ECMAScript中,引用类型是一种数据结构,用于将数据和功能组织在一起.在其他面向对象语言中被称为类,虽然 ...
- 在windows下使用linux的开发环境
windows下做开发确实有些不方便,比如python.ruby什么的都要自己装,不过这还是小事情.有一次想安装node-sass,windows下报错缺少MSBuild什么的,可能需要装一个vs解决 ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...