Selenium provides the following methods to locate elements in a page:

  • 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

To find multiple elements (these methods will return a list):

  • 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

Apart from the public methods given above, there are two private methods which might be useful with locators in page objects. These are the two private methods: find_element and find_elements.

Example usage:

  1. # Python
  2. from selenium.webdriver.common.by import By
  3.  
  4. driver.find_element(By.XPATH, '//button[text()="Some text"]')
  5. driver.find_elements(By.XPATH, '//button')

These are the attributes available for By class:

  1. ID = "id"
  2. XPATH = "xpath"
  3. LINK_TEXT = "link text"
  4. PARTIAL_LINK_TEXT = "partial link text"
  5. NAME = "name"
  6. TAG_NAME = "tag name"
  7. CLASS_NAME = "class name"
  8. CSS_SELECTOR = "css selector"

Locating by XPath

XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications. XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page.

One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.

Absolute XPaths contain the location of all elements from the root (html) and as a result are likely to fail with only the slightest adjustment to the application. By finding a nearby element with an id or name attribute (ideally a parent element) you can locate your target element based on the relationship. This is much less likely to change and can make your tests more robust.

  1. <html>
  2. <body>
  3. <form id="loginForm">
  4. <input name="username" type="text" />
  5. <input name="password" type="password" />
  6. <input name="continue" type="submit" value="Login" />
  7. <input name="continue" type="button" value="Clear" />
  8. </form>
  9. </body>
  10. <html>

The form elements can be located like this:

  1. login_form = driver.find_element_by_xpath("/html/body/form[1]")
  2. login_form = driver.find_element_by_xpath("//form[1]")
  3. login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
  1. Absolute path (would break if the HTML was changed only slightly)
  2. First form element in the HTML
  3. The form element with attribute named id and the value loginForm

The username element can be located like this:

  1. username = driver.find_element_by_xpath("//form[input/@name='username']")
  2. username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
  3. username = driver.find_element_by_xpath("//input[@name='username']")
  1. First form element with an input child element with attribute named name and the valueusername
  2. First input child element of the form element with attribute named id and the value loginForm
  3. First input element with attribute named ‘name’ and the value username

The “Clear” button element can be located like this:

  1. clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
  2. clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
  1. Input with attribute named name and the value continue and attribute named type and the valuebutton
  2. Fourth input child element of the form element with attribute named id and value loginForm

There are also a couple of very useful Add-ons that can assist in discovering the XPath of an element:

  • XPath Checker - suggests XPath and can be used to test XPath results.
  • Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.
  • XPath Helper - for Google Chrome

Selenium - WebDriver: Locating Elements的更多相关文章

  1. Selenium Tutorial (1) - Starting with Selenium WebDriver

    Starting with Selenium WebDriver Selenium WebDriver - Introduction & Features How Selenium WebDr ...

  2. [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍

    前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...

  3. Selenium Locating Elements

    Locating Elements Location Methods: find_element_by_id find_element_by_name find_element_by_xpath fi ...

  4. Selenium - WebDriver: Waits

    These days most of the web apps are using AJAX techniques. When a page is loaded to browser, the ele ...

  5. Selenium WebDriver Code

    Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...

  6. selenium webdriver 右键另存为下载文件(结合robot and autoIt)

    首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...

  7. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  8. Selenium Webdriver定位元素的几种方式

    原文:http://www.cnblogs.com/tobecrazy/p/4570494.html 工作中使用到记录一下. 主要有: 上传 alter dialog prompt dialog co ...

  9. Selenium - WebDriver: Page Objects

    This chapter is a tutorial introduction to page objects design pattern. A page object represents an ...

随机推荐

  1. HDU 1114 Piggy-Bank 猪仔储钱罐(完全背包)

    题意: 给定一个存钱罐中要存硬币,知道空罐的重量和欲装满的重量,是否能装入?若能,打印最小价值.(注:能装的硬币重量一定刚刚好,里面的总价值要达到最小) 输入: 包含了T个测试例子,在第一行给出.接下 ...

  2. ASP.NET中登陆验证码的生成和输入验证码的验证

    一:验证码的生成实现代码 protected void Page_Load(object sender, EventArgs e)    {        string validateCode = ...

  3. linux 命令——43 killall(转)

    Linux 系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进 程,如果要找到我们需要杀死的进程,我们还需 ...

  4. this.value = this.placeholder || this.getAttribute('placeholder')

    this.value = this.placeholder || this.getAttribute('placeholder') 鉴于不同的浏览器对为止属性的实现方式有所不用,这里同时使用了HTML ...

  5. Objective-C 引用计数原理

    http://www.cocoachina.com/ios/20160112/14933.html 引用计数如何存储 有些对象如果支持使用 TaggedPointer,苹果会直接将其指针值作为引用计数 ...

  6. 【BZOJ1257】[CQOI2007] 余数之和(数学题)

    点此看题面 大致题意: 求\(\sum_{i=1}^nk\%i\). 关于除法分块 这是一道除法分块的简单应用题. 式子转换 显然\(k\%i\)是一个很难处理的项. 于是我们就要使用使用一个常用的套 ...

  7. gearmand 编译 Unable to find libevent

    如果出现configure: error: Unable to find libevent,则输入命令:yum -y install libevent libevent-devel然后重新config ...

  8. 2017.12.23 第二章 统一建模语言UML概述

    第二章 统一建模语言UML概述 (1)为什么要建模 模型是某个事物的抽象,其目的是在构建这个事物之前先来理解它,因为模型忽略了那些非本质的细节,这样有利于更好的理解和表示事物: 在软件系统开发之前首先 ...

  9. CUDA常见问题与解答

    源 1.在SDK自带的例子程序中,发现SRC文件珜下有.cpp文件和.cu文件.这两种文件的关系和各自的作用是什么呀? 答:SDK自带例子中的.cpp文件主要是一些CPU端处理,或者是使用CPU计算对 ...

  10. SpringBoot2.X最佳实践《一》 之 SpringBoot2.x初体验

    SpringBoot2.X最佳实践 前言 本系列文章,从零基础接触  SpringBoot2.x新版本,基础入门使用,热部署,到整合各个主流框架Redis4.x,消息队列AciveMQ, Rocket ...