Locating Elements

Location Methods:

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

Example usage:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

By class attributes:

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"

1.Locating by Id

Page source:

<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

Page source:

<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 = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
continue = driver.find_element_by_name('continue') //"Login" button,it occures before the "Clear" button

3.Locating by XPath

Page source:

<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> //locating 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']")
//locating 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']")
//locating "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

Page source:

<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> 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

Page source:

<html>
<body>
<h1>Welcome</h1>
<p>Site content goes here.</p>
</body>
<html> heading1 = driver.find_element_by_tag_name('h1')

6.Locating Elements by Class Name

Page source:

<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html> content = driver.find_element_by_class_name('content') //Locating "p" element

7.Locating Elements by CSS Selectors

Page source:

<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html> content = driver.find_element_by_css_selector('p.content') ////Locating "p" element

节选自Selenium官网Doc:

https://selenium-python.readthedocs.io/locating-elements.html

Selenium Locating Elements的更多相关文章

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

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

  2. Selenium - WebDriver: Locating Elements

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

  3. <译>Selenium Python Bindings 4 - Locating Eelements

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

  4. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  5. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

    前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...

  6. selenium资料

    来源 http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.ISelenium.MouseMo ...

  7. Beautifulsoup 和selenium 的查询

    Selenium There are vaious strategies to locate elements in a page. You can use the most appropriate ...

  8. Python+Selenium+PIL+Tesseract真正自动识别验证码进行一键登录

    Python 2.7 IDE Pycharm 5.0.3 Selenium:Selenium的介绍及使用,强烈推荐@ Eastmount的博客 PIL : Pillow-3.3.0-cp27-cp27 ...

  9. [python爬虫] Selenium常见元素定位方法和操作的学习介绍

    这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...

随机推荐

  1. 一本通 1223:An Easy Problem

    \[传送门qwq\] [题目描述] 给定一个正整数N,求最小的.比N大的正整数M,使得M与N的二进制表示中有相同数目的1. 举个例子,假如给定的N为78,其二进制表示为1001110,包含4个1,那么 ...

  2. python的web运用

    ---恢复内容开始--- 对于大多数学过编程语言的人来说都知道大部分的编程语言都可以用来开发web运用,对于python来说也是可以的,不过对于web开发来说用python你可以选择 两个不同的框架, ...

  3. PS快速祛除脸上小雀斑

    首先我们要把图片放到PS软件中,然后在PS左侧工具栏中找到污点修复画笔工具(J), 配合着污点修复画笔中的修补工具一起使用,注意:模式要选择正常,属性栏中类型要选择内容识别. 下一步我们需要在图层上添 ...

  4. Zookeeper连接eclipse

    package com.bw.ZK; import java.io.IOException; import org.apache.zookeeper.CreateMode; import org.ap ...

  5. 计算机网络基础知识-OSI七层协议模型

    一.物理层 物理层主要规定了物理设备的标准,如网线的类型.光纤的接口类型.各种传输介质的传输速率,物理层的数据以比特流(二进制)的形式存在,传输时将比特流转化为电流强弱,达到目的地之后再转化为比特流. ...

  6. qrcode & vue

    qrcode & vue $ yarn add qrcode.vue # OR $ npm i -S qrcode.vue https://www.npmjs.com/package/qrco ...

  7. python json数据的转换

    1  Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...

  8. .class和.getClass()的区别

    使用指定类初始化日志对象,在日志输出的时候,可以打印出日志信息所在类 如: getClass() 返回此 Object 的运行时类. //需要有com.lpx.test.class这个类 Logger ...

  9. Announcing Microsoft Research Open Data – Datasets by Microsoft Research now available in the cloud

    The Microsoft Research Outreach team has worked extensively with the external research community to ...

  10. JarvisOJ Misc shell流量分析

    分析一下shell流量,得到flag 看着一大推的数据记录头都大了,并没有什么wireshark的使用经验,开始胡搞 首先用notepad++打开,搜索flag字样找到了一个类似于python脚本的东 ...