使用Python学习selenium测试工具-4:查找元素
转自:https://blog.csdn.net/wd168/article/details/51819930
web通常包含了Hyper Text Markup Language (HTML)、Cascading Style Sheets (CSS)和JavaScript。本节主要内容如下:
了解更多Selenium webDriver查找元素的知识
使用各种浏览器提供的开发工具找到和定位元素
多种发现元素的方法:ID、Name、类属性值、XPath、CSS选择器
Selenium webDriver中的各种find_element_by方法。
一般的浏览器都有查看源码的功能 。
使用开发工具发现定位器
Firefox的插件Firebug是个好帮手,可以F12或者右键点击元素选择“Inspect Element with Firebug”打开。Firefox的搜素框还可以对XPath或CSS进行查找。
Chrome中可以F12或者右键点击元素选择“Inspect Element”的方式查看代码。查找功能也和Firefox类似。Internet Explorer也有类似功能,不再赘述。
元素查找
find_element_by*系列方法在找到元素的时候返回WebElement实例,否则产生NoSuchElementException异常。另外find_elements_by*系列方法可以一次返回多个元素。
driver.find_element_by_css_selector(‘#search’)
| Method | Description | Argument | Example |
| find_element_by_id(id) | This method finds an element by the ID attribute value | id: The ID of the element to be found | driver.find_element_by_id(‘search’) |
| find_element_by_name(name) | This method finds an element by the name attribute value | name: The name of the element to be found | driver.find_element_by_name(‘q’) |
| find_element_by_class_name(name) | This method finds an element by the class attribute value | name: The class name of the element to be found | driver.find_element_by_class_name(‘input-text’) |
| find_element_by_tag_name(name) | This method finds an element by its tag name | name: The tag name of the element to be found | driver.find_element_by_tag_name(‘input’) |
| find_element_by_xpath(xpath) | This method finds an element using XPath | xpath: The xpath of the element to be found | driver.find_element_by_xpath(‘form[0]/div[0]/input[0]’) |
| find_element_by_css_selector(css_selector) | This method finds an element by the CSS selector | css_selector: The CSS selector of the element to be found | |
| find_element_by_link_text(link_text) | This method finds an element by the link text | link_text: The text of the element to be found | driver.find_element_by_link_text(‘Log In’) |
| find_element_by_partial_link_text(link_text) | This method finds an element by a partial match of its link text | link_text: The text to match part of the text of the element | driver.find_element_by_partial_link_text(‘Log’) |
通过id、name、类属性是最建议,也是最快速的查找方法,尤其是id和name。其次使用tag和链接文本,万不得已才使用xpath和css。
XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言。参考:XPath维基百科介绍 以及w3schools的Xpath介绍,zvon的XPath 1.0介绍。参考书籍:Selenium Testing Tools Cookbook。注意XPath的速度会比CSS还慢,不过支持向前向后定义和相对路径。
层叠样式表(英语:Cascading Style Sheets,简写CSS),又称串样式列表、层次结构式样式表文件,一种用来为结构化文档(如HTML文档或XML应用)添加样式(字体、间距和颜色等)的计算机语言。参考CSS维基百科介绍,w3schools之CSS介绍 和
实例
- import unittest
- from selenium import webdriver
- class HomePageTest(unittest.TestCase):
- @classmethod
- def setUpClass(cls):
- # create a new Firefox session
- cls.driver = webdriver.Firefox()
- cls.driver.implicitly_wait(30)
- cls.driver.maximize_window()
- # navigate to the application home page
- cls.driver.get('http://demo-store.seleniumacademy.com/')
- def test_search_text_field_max_length(self):
- # get the search textbox
- search_field = self.driver.find_element_by_id('search')
- # check maxlength attribute is set to 128
- self.assertEqual('128', search_field.get_attribute('maxlength'))
- def test_search_button_enabled(self):
- # get Search button
- search_button = self.driver.find_element_by_class_name('button')
- # check Search button is enabled
- self.assertTrue(search_button.is_enabled())
- def test_my_account_link_is_displayed(self):
- # get the Account link
- account_link = self.driver.find_element_by_link_text('ACCOUNT')
- # check My Account link is displayed/visible in the Home page footer
- self.assertTrue(account_link.is_displayed())
- def test_account_links(self):
- # get the all the links with Account text in it
- account_links = self.driver.\
- find_elements_by_partial_link_text('ACCOUNT')
- # check Account and My Account link is displayed/visible in the Home page footer
- self.assertTrue(len(account_links), 2)
- def test_count_of_promo_banners_images(self):
- # get promo banner list
- banner_list = self.driver.find_element_by_class_name('promos')
- # get images from the banner_list
- banners = banner_list.find_elements_by_tag_name('img')
- # check there are 3 banners displayed on the page
- self.assertEqual(3, len(banners))
- def test_vip_promo(self):
- # get vip promo image
- vip_promo = self.driver.\
- find_element_by_xpath("//img[@alt='Shop Private Sales - Members Only']")
- # check vip promo logo is displayed on home page
- self.assertTrue(vip_promo.is_displayed())
- # click on vip promo images to open the page
- vip_promo.click()
- # check page title
- self.assertEqual("VIP", self.driver.title)
- def test_shopping_cart_status(self):
- # check content of My Shopping Cart block on Home page
- # get the Shopping cart icon and click to open the Shopping Cart section
- shopping_cart_icon = self.driver.\
- find_element_by_css_selector('div.header-minicart span.icon')
- shopping_cart_icon.click()
- # get the shopping cart status
- shopping_cart_status = self.driver.\
- find_element_by_css_selector('p.empty').text
- self.assertEqual('You have no items in your shopping cart.',
- shopping_cart_status)
- # close the shopping cart section
- close_button = self.driver.\
- find_element_by_css_selector('div.minicart-wrapper a.close')
- close_button.click()
- @classmethod
- def tearDownClass(cls):
- # close the browser window
- cls.driver.quit()
- if __name__ == '__main__':
- unittest.main(verbosity=2)
执行结果:
# python homepagetest.py
test_account_links (__main__.HomePageTest) ... ok
test_count_of_promo_banners_images (__main__.HomePageTest) ... ok
test_my_account_link_is_displayed (__main__.HomePageTest) ... ok
test_search_button_enabled (__main__.HomePageTest) ... ok
test_search_text_field_max_length (__main__.HomePageTest) ... ok
test_shopping_cart_status (__main__.HomePageTest) ... ok
test_vip_promo (__main__.HomePageTest) ... ok
----------------------------------------------------------------------
Ran 7 tests in 77.086s
OK
使用Python学习selenium测试工具-4:查找元素的更多相关文章
- 【转】使用Python学习selenium测试工具
出处:https://my.oschina.net/u/1433482/blog/633231?fromerr=vaxqh9bn
- Python学习--Selenium模块学习(2)
Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...
- Python中的测试工具
当我们在写程序的时候,我们需要通过测试来验证程序是否出错或者存在问题,但是,编写大量的测试来确保程序的每个细节都没问题会显得很繁琐.在Python中,我们可以借助一些标准模块来帮助我们自动完成测试 ...
- Python学习--Selenium模块
1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台
- 基于Python的XSS测试工具XSStrike使用方法
基于Python的XSS测试工具XSStrike使用方法 简介 XSStrike 是一款用于探测并利用XSS漏洞的脚本 XSStrike目前所提供的产品特性: 对参数进行模糊测试之后构建合适的payl ...
- Python学习--Selenium模块简单介绍(1)
简介及运行流程 Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozi ...
- 【python驱动】python进行selenium测试时GeckoDriver放在什么地方?
背景:用python进行selenium 关于b/s架构的测试,需要配置驱动否则程序无法执行 情况1:windows下放置GeckoDriver 步骤1:下载驱动 GeckoDriver下载地址fir ...
- python:selenium测试登录在chrome中闪退
问题描述:使用selenium.webdriver时测试网页,进行自动登录测试总是在登录成功时闪退.使用指定驱动器位置的方式chrome也会闪退 1.正常使用chrome驱动打开一个网页,正常访问 f ...
- selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码
目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...
随机推荐
- excel导出的时候从程序后台写到excel里的是文本,所以无法在excel中计算怎么办?
文章引用自:http://www.cnblogs.com/rayray/p/3414452.html excel导出的时候从程序后台写到excel里的是文本,所以无法在excel中计算怎么办? 需要导 ...
- 关于z-index的那些事儿
关于z-index的真正问题是,很少有人理解它到底是怎么用.其实它并不复杂,但是如果你从来没有花一定时间去看具体的z-index相关文档,那么你很可能会忽略一些重要的信息. 不相信我吗?好吧,看看你能 ...
- java I/O系统 LineNumberReader类
LineNumbeReader类可以很方便的读取文件的行号 package ch13; import java.io.*; import io.BufferedInputFile; public cl ...
- Crack相关
Microsoft Office 2007专业增强版密钥:KXFDR-7PTMK-YKYHD-C8FWV-BBPVWM7YXX-XJ8YH-WY349-4HPR9-4JBYJCTKXX-M97FT-8 ...
- mysql的基本演示
数据库需要配置 cmd打开doc窗口 net start mysql:启动数据库 net stop mysql :停止数据库 表的定义:列 行 主键
- springmvc文件上传下载简单实现案例(ssm框架使用)
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...
- C# Winform OpenFileDialog 控件
OpenFileDialog控件又称打开文件对话框,主要用来弹出Windows中标准的[打开文件]对话框. OpenFileDialog控件的常用属性如下. (1)Title属性:用来获取或设置对话框 ...
- BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
- P2700 逐个击破 最小生成树
题目描述 现在有N个城市,其中K个被敌方军团占领了,N个城市间有N-1条公路相连,破坏其中某条公路的代价是已知的,现在,告诉你K个敌方军团所在的城市,以及所有公路破坏的代价,请你算出花费最少的代价将这 ...
- 051 日志案例分析(PV,UV),以及动态分区
1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...