【Selenium专题】元素定位之CssSelector

CssSelector是我最喜欢的元素定位方法,Selenium官网的Document里极力推荐使用CSS locator,而不是XPath来定位元素,原因是CSS locator比XPath locator速度快,特别是在IE下面(IE没有自己的XPath 解析器(Parser))他比xpath更高效更准确更易编写,美中不足是根据页面文字时略有缺陷没有xpath直接。

因为前端开发人员就是用CSS Selector设置页面上每一个元素的样式,无论那个元素的位置有多复杂,他们能定位到,那我们使用CSS Selector肯定也能非常精准的定位到页面Elements。

CssSelector常用定位

1.根据tagName

  driver.findElement(By.cssSelector("input")

2.根据ID

  driver.findElement(By.cssSelector("input#username"));html标签和#id

  driver.findElement(By.cssSelector("#username"));只是#id

3.根据className

  单一class:driver.findElement(By.cssSelector(".username"));.class

  复合class:driver.findElement(By.cssSelector(".username.**.***"));.classA.classB

4.根据元素属性

  1)精准匹配:

    [A]  driver.findElement(By.cssSelector("input[name=username]"));属性名=属性值,id,class,等都可写成这种形式

[B]  driver.findElement(By.cssSelector("img[alt]"));存在属性。例如img元素存在alt属性

    [C]  driver.findElement(By.cssSelector("input[type=‘submit‘][value=‘Login‘]"));多属性

  2)模糊匹配:(正则表达式匹配属性)

    [A]  ^=  driver.findElement(By.cssSelector(Input[id ^=‘ctrl‘]));匹配到id头部 如ctrl_12

    [B]  $=  driver.findElement(By.cssSelector(Input[id $=‘ctrl‘]));匹配到id尾部 如a_ctrl

    [C]  *=  driver.findElement(By.cssSelector(Input[id *= ‘ctrl‘]));匹配到id中间如1_ctrl_12

    更多正则匹配原则请查看CSS3 选择器——属性选择器  http://www.w3cplus.com/css3/attribute-selectors

5.查询子元素

<form id="form" class="fm" name="f">
  <span id="s_kw_wrap" class="bg s_ipt_wr quickdelete-wrap">
    <input id="kw" class="s_ipt" type="text" autocomplete="off" maxlength="100" name="wd">
  </span>
  <span id="s_btn_wr" class="btn_wr s_btn_wr bg">
    <input id="su" class="btn self-btn bg s_btn" type="submit" value="百度一下">
  </span>
</form>

案例html

以上代码是百度首页搜索输入框和按钮的html,下面讲解以此为例

  1)子元素   A>B

    WebElement input=  driver.findElement(By.cssSelector("form>span>input"));//搜索输入框

  2)后代元素   A空格B

    WebElement input=  driver.findElement(By.cssSelector("form input"));//搜索输入框

  3)第一个后代元素  :first-child

    WebElement span= driver.findEleme(By.cssSelector("form :first-child"));//冒号前有空格,定位到form下所有级别的第一个子元素

      可定位到三个元素:<span id="s_kw_wrap".../> <input id="kw"..../> <input id="su"........./>

    WebElement span= driver.findEleme(By.cssSelector("form input:first-child"));//冒号前无空格,定位到form下所有级别的第一个input元素

      可定位到两个元素:<input id="kw"..../> <input id="su"........./>

    WebElement span= driver.findEleme(By.cssSelector("form>span:first-child"));//冒号前无空格,定位到form直接子元素中的第一个span元素

      可定位到一个元素:<span id="s_kw_wrap".../>

  4)最后一个子元素   :last-child  [类同:first-child]

    WebElement userName = driver.findEleme(By.cssSelector("form :last-child"));//冒号前有空格,定位到form下所有级别的第一个子元素

  5)第2个子元素    :nth-child(N)  [类同:first-child]

    WebElement userName = driver.findEleme(By.cssSelector("form#form :nth-child(2)"));//冒号前有空格,定位到form下所有级别的第二个子元素

6.查询兄弟元素

  driver.findElement(By.cssSelector("form#form span+span")); 定位到a 再定位到和它相邻的b

深入学习cssselector可访问以下地址:



http://www.w3.org/TR/css3-selectors/

CSS3 选择器——基本选择器  http://www.w3cplus.com/css3/basic-selectors
CSS3 选择器——属性选择器  http://www.w3cplus.com/css3/attribute-selectors
CSS3 选择器——伪类选择器  http://www.w3cplus.com/css3/pseudo-class-selector
 
时间: 2024-08-28 07:04:33

【Selenium专题】元素定位之CssSelector的相关文章

selenium中元素定位的常用方法

一.Selenium中元素定位共有八种 id name className tagName linkText partialLinkText xpath cssSelector 其中前六种都比较简单,通过id一般可以得到一个唯一的定位,其他五种要注意元素是否唯一,如果有多个,返回的是匹配的第一个元素.下面详细讲解一下xpath和cssSelector定位. 二.xpath定位 1.通过绝对路径定位 dr.findElement(By.xpath("/html/body/div[2]/div/di

Selenium Webdriver元素定位的方式

Selenium Webdriver元素定位的方式 主要就是By类的 1.By.name() ## html代码如下: <button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>

Web自动化测试 四 ----- python selenium 八大元素定位

python selenium 八大元素定位方法 前提条件:先要引入webdriver模块,创建一个Chrom浏览器对象,以及打开一个网页(以百度为例). 1 from selenium import webdriver 2 3 driver = webdriver.Chrome() 4 5 driver.get('http://www.baidu.com') 一.ID定位 driver.find_element_by_id('kw') 备注: 此方法相当于JS中的getElementById(

Selenium Webdriver元素定位的八种常用方式

在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素.其中By类的常用定位方式共八种,现分别介绍如下. 1. By.name() 假设我们要测试的页面源码如下: <button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><

java selenium (五) 元素定位大全

页面元素定位是自动化中最重要的事情, selenium Webdriver 提供了很多种元素定位的方法.  测试人员应该熟练掌握各种定位方法. 使用最简单,最稳定的定位方法. 阅读目录 自动化测试步骤 在自动化测试过程中, 测试程序通常的操作页面元素步骤 1. 找到Web的页面元素,并赋予到一个存储对象中 (WebElement) 2. 对存储页面元素的对象进行操作, 例如:点击链接,在输入框中输入字符等 3. 验证页面上的元素是否符合预期 通过这三个步骤, 我们可以完成一个页面元素的操作, 找

Selenium Webdriver元素定位的八种常用方式(转载)

转自:http://www.cnblogs.com/qingchunjun/p/4208159.html 在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素.其中By类的常用定位方式共八种,现分别介绍如下. 1. By.name() 假设我们要测试的页面源码如下: <button id="gbqfba" aria-label="Google Search" n

3 Python+Selenium的元素定位方法(id、class name、name、tag name)

[环境] Python3.6+selenium3.0.2+IE11+Win7 [定位方法] 1.通过ID定位 方法:find_element_by_id('xx') 2.通过name定位 方法:find_element_by_name('xx') 3.通过class name定位 方法:find_element_by_class_name('xx') 4.通过tag name定位 方法:find_element_by_tag_name('xx') 说明:tag name在html中是标签的名字,

selenium对元素定位和操作方法的封装

前面介绍了selenium对元素的定位方法和操作方法,但是因为这些方法用起来重复的次数比较多,所以我们在下面对他们进行一个封装 # -*- coding:UTF-8 -*- from selenium import webdriver import time class common(object): #新建对象就自动创建浏览器并且最大化窗口 def __init__(self): self.driver=webdriver.Chrome() self.driver.maximize_windo

selenium浏览器元素定位

在元素定位中xpath使用的还算比较多,介绍一下常见的firfox和chrome浏览器插件安装 1.firfox firfox比较简单,主要浏览器自带的定位功能也比较强大国内也比较好的支持插件安装 1)安装 我这里已经安装好了所以可以在"我的附加组件"里面查看,如果没有安装的话可以在"可用附加组件"中安装 2)使用 firepath和firefinder都可以定位元素,firepath可以在定位元素的时候生成xpath或者css,finefinder比较偏向验证xp