• 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方法使用的更多相关文章

  1. Selenium+Python常见定位方法

    参见官网:http://selenium-python.readthedocs.io/locating-elements.html 有多种策略来定位页面中的元素.你可以使用最适合你的情况.Seleni ...

  2. selenium+python自动化测试

    F12: 右键   选择复制  path 在selenium+python自动化测试(一)–环境搭建中,运行了一个测试脚本,脚本内容如下: from selenium import webdriver ...

  3. selenium + python自动化测试unittest框架学习(五)webdriver的二次封装

    因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<se ...

  4. selenium+python+unittest实现自动化测试(入门篇)

    本文主要讲解关于selenium自动化测试框架的入门知识点,教大家如何搭建selenium自动化测试环境,如何用selenium+python+unittest实现web页面的自动化测试,先来看看se ...

  5. selenium + python 自动化测试环境搭建

    selenium + python 自动化测试 —— 环境搭建 关于 selenium Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操 ...

  6. <译>Selenium Python Bindings 5 - Waits

    如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...

  7. <译>Selenium Python Bindings 2 - Getting Started

    Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...

  8. <译>Selenium Python Bindings 1 - Installation

    Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...

  9. selenium python 第一个脚本

    为什么选择python?我的回答很简单,简单易学,功能强大! 下面看看python和selenium 2的结合是什么样子吧 一.第一个selenium python脚本: #coding = utf- ...

随机推荐

  1. 安装 ant

    (1)下载ant:http://ant.apache.org/bindownload.cgi (2)解压后放在指定的文件夹中(/usr/lib) (3) sudo chmod 777 /usr/lib ...

  2. web前端开发(5)

    CSS的一些问题: 一般情况下,尽量使用class选择器 解决点击超链接后hover 样式不出现多次问题:a:visited  a:hover 的顺序是问题所在,记住 love hate  L(lin ...

  3. Linux下命令sort, uniq

    标题:sort, uniq 一.sort命令的使用       1. 作用:sort命令顾名思意,其可以帮助我们进行排序,而且可以依据不同的数据类型来排序.例如数字和文字的排序就不一样,sort可以指 ...

  4. Python之时间统计

    1. import time start_time = time.time() print('time %ds'%(time.time() - start_time))

  5. Table of Contents - JavaSE

    Java Bean Lombok IO Commons-IO - IOUtils Regular Expression 正则表达式常用操作 Concurrency java.util.concurre ...

  6. MongoDB - The mongo Shell, Access the mongo Shell Help

    In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional inf ...

  7. 【CSS3】---曲线阴影翘边阴影

    效果图 代码 index <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title& ...

  8. ASP.NET缓存全解析7:第三方分布式缓存解决方案 Memcached和Cacheman 转自网络原文作者李天平

    Memcached — 分布式缓存系统 1.Memcached是什么? Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.Memcached通过在内 ...

  9. 2015影响因子Excel版

    现在终于有影响因子2015的Excel版了,这个版本除了还包括杂志全称和缩写等基本信息还,包括中科院分区, 应该是迄今2015影响因子最奢华的版本了吧. 看了这个版本,发现中国的SCI杂志还是不少的, ...

  10. gulp基础使用总结

    gulp 安装 1 检测电脑有没有安装node 执行 $ node -v $ npm -v 如果没有安装的话,可以到https://nodejs.org/en/download/下载安装. 2 全局安 ...