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. git中如何撤销部分修改?

    以提问中修改了两个文件a.b为例,假设需要撤销文件a的修改,则修改后的两个文件: 1.如果没有被git add到索引区 git checkout a 便可撤销对文件a的修改 2.如果被git add到 ...

  2. JDBC连接池之C3P0

    1.导入jar包 c3p0-0.9.1.jar mchange-commons-java-0.2.3.4(注:该jar包是c3p0数据库连接池的辅助包,没有这个包系统启动的时候会报classnotfo ...

  3. Redis的发布与订阅

    业务: 运用数据与信息指导小药工的采购生产与销售行为 需求背景: (1)药工汇小程序用户(即小型中药初加工用户)需要知道自己加工的品种的价格涨跌信息和品种相关资讯) 需求分析拆解: (1)使用爬虫程序 ...

  4. flask 实现登录 登出 检查登录状态 的两种方法的总结

    这里我是根据两个项目的实际情况做的总结,方法一(来自项目一)的登录用的是用户名(字符串)和密码,前后端不分离,用form表单传递数据:方法二用的是手机号和密码登录,前后端分离,以json格式传递数据, ...

  5. XCTF体验题库 : ReverseMe-120

    ida打开看一下: sub_401000函数是能否输出“correct”的关键 点进去看一下: 可以看到将输入的字符串赋予了byte_414E40这个数组的值,看一下这个数组: 应该是base64的解 ...

  6. Nginx 处理Http请求简单流程

    L45 1:三次握手后 系统内核收到请求根据端口负载均衡的分配到某个worker 2:nginx 会分配一个512byte链接内存池 3:初始化nginx的http模块并等待用户请求,假设用户在cli ...

  7. 自己实现ArrayList与LinkedList类

    ArrayList与LinkedList的底层实现 ArrayList内部由数组实现,LinkedList内部由链表实现. 自己动手实现ArrayList与LinkedList中一些常用方法 Arra ...

  8. Manacher算法详解

    问题 什么是回文串,如果一个字符串正着度读和反着读是一样的,这个字符串就被称为回文串. such as noon level aaa bbb 既然有了回文,那就要有关于回文的问题,于是就有了-- 最长 ...

  9. Java连接远程Mysql过程中遇到的各种问题

    2018-11-16 10:46 2018-11-19 21:35 前言 本篇文章记录的是本人在使用Java程序连接另一台电脑(同一局域网)上的Mysql数据库的过程中遇到的各种问题及解决方案.希望能 ...

  10. vsftp配置文件详解

    匿名用户相关参数 anonymous_enable=YES # 是否允许匿名用户登陆 no_anon_password=NO # 是否忽略对匿名用户的密码检测 anon_root # 匿名登陆后尝试更 ...