有各种不同的策略来定位页面中的元素。你可以使用最合适定位方式用于你的用例。Selenium提供了以下方法来定位页面中的元素:

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

  为了找到多个元素(这些方法会返回一个列表):

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector
  1. Locating by ID
    当你知道一个元素的id属性,请使用方法。在此策略下,与id属性值相匹配的位置的第一个元素将被返回。如果没有元素能匹配id属性,一个NoSuchElementException异常将抛出。
    为了测试,考虑这个页面的源代码:

    <html>
     <body>
      <form id="loginForm">
       <input name="username" type="text" />
       <input name="password" type="password" />
       <input name="continue" type="submit" value="Login" />
      </form>
     </body>
    <html>

    表格元素可以这样定位:

    login_form = driver.find_element_by_id('loginForm')
  2. Locating by Name
    当你知道一个元素的name属性,请使用方法。在此策略下,与name属性值相匹配的位置的第一个元素将被返回。如果没有元素能匹配name属性,一个NoSuchElementException异常将抛出。
    为了测试,考虑这个页面的源代码:
    <html>
     <body>
      <form id="loginForm">
       <input name="username" type="text" />
       <input name="password" type="password" />
       <input name="continue" type="submit" value="Login" />
       <input name="continue" type="button" value="Clear" />
      </form>
    </body>
    <html>

    username & password 元素可以这样定位:

    username = driver.find_element_by_name('username')
    password = driver.find_element_by_name('password')

    页面中第一个"continue"会被先执行(当元素的name一样时,先被定位的先执行),代码如下:

    continue = driver.find_element_by_name('continue')
  3. Locating by XPath

    XPath是一种用于在XML文档中定位节点使用的语言。HTML可以当作是XML ( XHTML )的实现,Selenium用户可以利用这个强大的语言定位他们Web应用程序中的元素。 XPath继承超出(以及配套)由id或name属性定位的简单方法,并开辟了各种新的可能性,如定位页面上的第三个复选框。

    使用XPath最主要的原因是,当你想定位元素没有合适的id或name属性。您可以使用XPath定位元素的绝对路径(不建议) ,或相对于确实有一个id或name属性的元素。 XPath定位,也可用于其他指定的其他元素。

    绝对的XPath包含从根(HTML)的所有元素的位置,其结果有可能会失败。通过查找附近有一个id或name属性的元素,你可以根据关系找到您的目标元素。这是不太可能改变的关系,可以让你的测试更强大。

    为了测试,考虑这个页面的源代码:

    <html>
     <body>
      <form id="loginForm">
       <input name="username" type="text" />
       <input name="password" type="password" />
       <input name="continue" type="submit" value="Login" />
       <input name="continue" type="button" value="Clear" />
      </form>
    </body>
    <html>

    页面form元素可以这样定位:

    login_form = driver.find_element_by_xpath("/html/body/form[1]")
    login_form = driver.find_element_by_xpath("//form[1]")
    login_form = driver.find_element_by_xpath("//form[@id='loginForm']")

    ①绝对路径

    ②页面HTML中第一个form
    ③form元素的id属性

    username 元素可以这样定位:

    username = driver.find_element_by_xpath("//form[input/@name='username']")
    username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
    username = driver.find_element_by_xpath("//input[@name='username']")

    clear 按钮可以这样定位:

    clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
    clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
  4. Locating Hyperlinks by Link Text
    当你知道一个锚标签中使用的链接文本可以使用此方法。在此策略下,与链接文本值相匹配的位置的第一个元素将被返回。如果没有元素能匹配name属性,一个NoSuchElementException异常将抛出。
    测试用HTML代码如下:
    <html>
     <body>
      <p>Are you sure you want to do this?</p>
      <a href="continue.html">Continue</a>
      <a href="cancel.html">Cancel</a>
    </body>
    <html>

    可以使用下面的方法来定位link:

    continue_link = driver.find_element_by_link_text('Continue')
    continue_link = driver.find_element_by_partial_link_text('Conti')
  5. Locating Elements by Tag Name
    当你想使用标签定位元素的时候,你可以使用此方法。使用此策略,第一个和元素匹配的标签将被返回,如果没有,则会异常抛出:
    测试用HTML代码:
    <html>
     <body>
      <h1>Welcome</h1>
      <p>Site content goes here.</p>
    </body>
    <html>

    当你想定位(h1)元素的时候,你可以这样定位:

    heading1 = driver.find_element_by_tag_name('h1')
  6. Locating Elements by Class name
    当你想使用元素class属性名定位的时候,你可以使用此策略。页面第一个匹配的class 名将被返回,如果没有匹配的class name,则会抛出异常:
    测试用HTML代码:
    <html>
     <body>
      <p class="content">Site content goes here.</p>
    </body>
    <html>

    p标签定位方法如下:

    content = driver.find_element_by_class_name('content')
  7. Locating Elements by CSS Selectors
    当你想通过CSS选择器定位元素的时候,你可以使用此策略,如果没有匹配的css选择器,那么异常将会抛出:
    测试用HTML代码如下:
    <html>
     <body>
      <p class="content">Site content goes here.</p>
    </body>
    <html>

    定位方法如下:

    content = driver.find_element_by_css_selector('p.content')

<译>Selenium Python Bindings 4 - Locating Eelements的更多相关文章

  1. <译>Selenium Python Bindings 1 - Installation

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

  2. <译>Selenium Python Bindings 5 - Waits

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

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

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

  4. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  5. <译>Selenium Python Bindings 3 - Navigating

    当你想要通过webdriver导航到一个链接,正常的方式点是通过调用get方法: driver.get("http://www.google.com") Interacting w ...

  6. selenium python bindings 元素定位

    1. 辅助 Firepath Firefox是所有做前端的必不可少的浏览器因为firebug的页面元素显示很清晰.用selenium 去定位元素的时候Firefox还有一个非常友好的工具就是firep ...

  7. [译]Selenium Python文档:目录

    作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...

  8. [译]Selenium Python文档:一、安装

    1.1.简介 Selenium Python为使用Selenium WebDriver来编写功能/验证测试提供了一个简单的API接口.通过Selenium Python API,你可以以一种非常直观的 ...

  9. [译]Selenium Python文档:二、初步开始

    2.1.简单使用 如果已经安装好了Selenium Python,你就可以像下面这样编写Python代码来使用它了: from selenium import webdriver from selen ...

随机推荐

  1. VS2010彻底卸载

    下载Microsoft Visual Studio 2010 Uninstall Utility来移除,默认情况下,这将删除 Visual Studio 和支持组件,但不会删除与计算机上的其他应用程序 ...

  2. HEOI2016游记

    DAY -1: 省选前集训因为某些事情感觉心情和状态异常的糟糕 坐在窗户上吹了半个小时的冷风之后觉得回家休息一段时间 (反正我家在保定,大不了我省选的时候直接去河北大学就好辣) 在家里颓了三天吧,想清 ...

  3. React测试Mixin

    1.test.jsx var randomNumberMixin = require("./randomNumberMixin.jsx"); describe("test ...

  4. 安装nginx创建错误

    ./configure: error: the HTTP gzip module requires the zlib library. 解决: yum install -y zlib-devel -- ...

  5. 1、搭建springMVC开发环境以及HelloWorld测试

    一.下载spring-framework,采用简单的方式: http://repo.springsource.org/libs-release-local/org/springframework/sp ...

  6. Unity获取方法的参数和方法名称

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { Console.Write ...

  7. R: count number of distinct values in a vector

    numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,         453,435,324,34,456,56,567,65,34,435) a & ...

  8. Java Swing中Substance个人比较喜欢的两种组合

    try { // 设置外形装饰为可装饰 JFrame.setDefaultLookAndFeelDecorated(true); // 设置外观 UIManager.setLookAndFeel(ne ...

  9. IE css expression(表达式)

    很多时候我们需要对IE6的bug写一些hack,如max-height,absolute元素高度100%等. css里面的 expression(表达式)和js里面的差不多,如: 获取当前元素的高度: ...

  10. 1450. Russian Pipelines(spfa)

    1450 水题 最长路 #include <iostream> #include<cstdio> #include<cstring> #include<alg ...