Selenium - WebDriver: Locating Elements
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:
# Python
from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
These are the attributes available for By class:
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
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.
<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>
The form elements can be located like this:
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']")
- Absolute path (would break if the HTML was changed only slightly)
- First form element in the HTML
- The form element with attribute named id and the value loginForm
The username element can be located like this:
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']")
- First form element with an input child element with attribute named name and the valueusername
- First input child element of the form element with attribute named id and the value loginForm
- First input element with attribute named ‘name’ and the value username
The “Clear” button element can be located like this:
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
- Input with attribute named name and the value continue and attribute named type and the valuebutton
- 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的更多相关文章
- Selenium Tutorial (1) - Starting with Selenium WebDriver
Starting with Selenium WebDriver Selenium WebDriver - Introduction & Features How Selenium WebDr ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- Selenium Locating Elements
Locating Elements Location Methods: find_element_by_id find_element_by_name find_element_by_xpath fi ...
- Selenium - WebDriver: Waits
These days most of the web apps are using AJAX techniques. When a page is loaded to browser, the ele ...
- Selenium WebDriver Code
Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...
- Selenium_用selenium webdriver实现selenium RC中的类似的方法
最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...
- Selenium Webdriver定位元素的几种方式
原文:http://www.cnblogs.com/tobecrazy/p/4570494.html 工作中使用到记录一下. 主要有: 上传 alter dialog prompt dialog co ...
- Selenium - WebDriver: Page Objects
This chapter is a tutorial introduction to page objects design pattern. A page object represents an ...
随机推荐
- IOS Prefix.pch程序常见文件 的作用
#import <Availability.h> #ifndef __IPHONE_5_0 #warning "This project uses features only a ...
- Django疑难问题
1页面出现中文报错 :Non-ASCII character '\xe9' in file E:\CPaas\cpaas\views.py 解决:在页面顶部加入#coding=utf-8 2执行syn ...
- UVALive 3530 Martian Mining(贪心,dp)
分析: 对于网格grid[i][j]如果放向上的管道,那么grid[i][k], k>j 就只能放向上的管道了. 那么定义dp[i][j]表示第i行,最后一个放向左的管道是j的最大总矿量. j ...
- NOIP2018赛前停课集训记——最后的刷板子计划
前言 再过两天就\(NOIP2018\)了. 于是,我决定不做其他题目,开始一心一意刷板子了. 这篇博客记录的就是我的刷板子计划. [洛谷3383][模板]线性筛素数 这种普及-的题目我还写挂了两次( ...
- 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)
点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...
- 获取屏幕上的某个控件相对位置,尤其是tableviewcell上的某一个控件的相对位置
我的需求就是tableviewcell上的按钮,点击就会出现一个弹框: 主要就是获取,所点击的cell上控件的相对位置: CGPoint buttonCenter = CGPointMake(btn. ...
- 在ListBox控件间交换数据
实现效果: 知识运用: ListBox控件的SelectedItem属性 //获取或设置ListBox控件中当前选定的数据项 public Object SelectedItem{ get;set; ...
- 2018.5.28 Oracle数据库补充
select * from (select rownum rn,e2.* from (select e1.* from emp e1)e2 where rownum<=10)e3 where e ...
- cookie 和 localStorage 、sessionStorage、 session不同
1. cookie:存储大小4k 有时间限制,会跟在ajax的请求头上 2. localStorage: 存储大小5M 没有时间限制 3. sessionStorage: 临时会话存储 当浏览器关闭的 ...
- 数据结构期末复习( はち)--VOA图关键路径求法
题目如下图: 注:将123456当成abcdef. 事件最早发生事件求法:找从原点到该事件的最长路径(从前往后推) 对a:Ve=0 对b:Ve=max{ 2 , 15+4 }=19 对c:Ve=15 ...