Selenium with Python 003 - 页面元素定位
WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法。
定位单个页面元素(返回单个元素对象)
- 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
需要注意的是,上面的方法当匹配到多个对象时,只能返回定位到的第一个元素对象,当没有匹配到任何元素对象,则会抛出异常NoSuchElementException
定位页面元素组(返回元素对象列表)
- 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
HTML模板Example1
<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>
通过id定位
login_form = driver.find_element_by_id('loginForm')
通过name定位
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
通过xpath定位
1.利用绝对路径定位
login_form = driver.find_element_by_xpath("/html/body/form[1]")
2.利用元素属性定位
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
3.层级与属性结合
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
4.使用逻辑运算符
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//input[@name='continue' and @type='button']")
HTML模板Example2
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Welcome</h1>
<p class="content">Are you sure you want to do this?</p>
<div id="detail">
<a href="continue.html" name="tj_continue" title="web" class="RecycleBin xz"
id="continue">Continue</a>
<a href="cancel.html" target="_blank">Cancel</a>
</div> <a href="" name="index" target="_blank">index</a> </body>
<html>
通过链接文本定位
continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
通过标签名定位
heading1 = driver.find_element_by_tag_name('h1')
通过class定位
content = driver.find_element_by_class_name('content')
通过css 选择器定位

1.通过class属性定位
content = driver.find_element_by_css_selector('p.content')
continue_link = driver.find_element_by_css_selector('.RecycleBin.xz')
continue_link = driver.find_element_by_css_selector('a.RecycleBin')
2.通过id属性定位
continue_link = driver.find_element_by_css_selector('#continue')
3.通过标签名定位
links = driver.find_elements_by_css_selector('a')
4.通过父子关系定位
links=driver.find_elements_by_css_selector('div>a')
5.通过兄弟关系定位
links=driver.find_elements_by_css_selector('div+a')
6.通过属性定位
links=driver.find_elements_by_css_selector("[target=_blank]")
continue_link = driver.find_element_by_css_selector('a[title="web"]')
7.组合定位
driver.find_element_by_css_selector('div#detail>a.RecycleBin')
driver.find_element_by_css_selector('div#detail>a[name=tj_continue]')
from selenium import webdriver
import os driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('demo.html')
driver.get(file_path) print(driver.find_element_by_css_selector('div#detail>a.RecycleBin').text)
print(driver.find_element_by_css_selector('div#detail>a[name=tj_continue]').text) driver.close()
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
Selenium with Python 003 - 页面元素定位的更多相关文章
- java selenium webdriver第二讲 页面元素定位
自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...
- Selenium with Python 004 - 页面元素操作
毫无疑问,首先需要导入webdriver from selenium import webdriver 清除文本 driver.find_element_by_id('kw').clear() 文本输 ...
- selenium常用命令之页面元素定位
WebDriver driver= new ChromeDriver(); <input type="text" id="phone" name=&q ...
- Python3.x:Selenium中的webdriver进行页面元素定位
Python3.x:Selenium中的webdriver进行页面元素定位 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素属性(class属性,name属性)等等.webdriver ...
- java selenium webdriver实战 页面元素定位
自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...
- 页面元素定位 XPath 简介
页面元素定位 XPath 简介 本文所说的 Xpath 是用于 Selenium 自动化测试所使用到的,是针对XHTML网页而言的一种页面元素的定位表示法. XPath 背景 XPath即为XML路径 ...
- Robot Framework 教程 (2) - 页面元素定位
上一篇文章中,简单模拟了一个baidu搜索并截图的过程,在搜索过程中,我们需要对搜索框.搜索按钮进行定位.本篇文章主要介绍一下具体的定位方法. 我们的脚本如下: *** Settings *** Li ...
- 5、通过Appium Desktop实现页面元素定位
之前我们已经安装了Appium Desktop,下面就让我们使用Appium Desktop实现页面元素定位 1.首先我们打开Appium Desktop,进入如下界面,点击Start Server ...
- selenium+python自动化之元素定位
自动化按步骤拆分的话,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇接下来讲基本的八种元素定位方法.说的通俗一点,就是教大家找对象. ...
随机推荐
- ArcGIS api for silverlight 禁用默认浏览操作
ArcGIS api for silverlight 的mapcontrol中提供了一系列的默认浏览工具选项(default navigation options),如下表所示, 那么如何禁用这些默认 ...
- 深入Redis内部-Redis 源码讲解(转)
Redis作为 NoSQL 数据库的杰出代表,一直广受关注,其轻量级的敏捷架构,向来有存储中的瑞士军刀之称.下面推荐的一篇文章,从源码的角度讲解了Redis 的整个工作流程,是了解 Redis 流程的 ...
- SSDT表概念具体解释
SSDT 的全称是 System Services Descriptor Table,系统服务描写叙述符表. 这个表就是一个把 Ring3 的 Win32 API 和 Ring0 的内核 API 联系 ...
- CoreThink开发(十)把官方首页轮播替换成HTML5-3D轮播
效果: 资源已经上传到我的下载里边. http://download.csdn.net/detail/u012995856/9587206 1.复制资源文件到CoreThink项目中 corethin ...
- Python-Cpython解释器支持的进程与线程
一.Python并发编程之多进程 1. multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyt ...
- 设置SVN提交日志必填
1.打开visualSVN Server2.打开Repositories3.右键需要控制的项目->所有任务->manage hooks...4.选择Hooks tab,点击Pre-comm ...
- Entity FrameWork Code First 之 MVC4 数据库初始化策略用法
通过启用迁移和更新数据库可以很容易的生成一张表.但是对数据库修改之后,通过数据迁移就没那么好实现了. 这里用到数据库生成策略,进行对数据库操作: 一.3种主要数据库生成策略 1 CreateDatab ...
- SpringBoot服务器压测对比(jetty、tomcat、undertow)
1.本次对比基础环境信息如下: springboot版本1.5.10 centos虚机4c6G,版本7.4 centos实机2u16c40G,版本7.4,虚机运行在实机上 ab版本2.3 jprofi ...
- 20145229吴姗珊逆向BOF实践
20145229吴姗珊逆向BOF实践 实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ...
- mysql 5.6 设置root初始密码正确步骤,避免入坑
http://blog.csdn.net/lw_power/article/details/47368167