转自: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介绍 和

实例

  1. import unittest
  2. from selenium import webdriver
  3. class HomePageTest(unittest.TestCase):
  4. @classmethod
  5. def setUpClass(cls):
  6. # create a new Firefox session
  7. cls.driver = webdriver.Firefox()
  8. cls.driver.implicitly_wait(30)
  9. cls.driver.maximize_window()
  10. # navigate to the application home page
  11. cls.driver.get('http://demo-store.seleniumacademy.com/')
  12. def test_search_text_field_max_length(self):
  13. # get the search textbox
  14. search_field = self.driver.find_element_by_id('search')
  15. # check maxlength attribute is set to 128
  16. self.assertEqual('128', search_field.get_attribute('maxlength'))
  17. def test_search_button_enabled(self):
  18. # get Search button
  19. search_button = self.driver.find_element_by_class_name('button')
  20. # check Search button is enabled
  21. self.assertTrue(search_button.is_enabled())
  22. def test_my_account_link_is_displayed(self):
  23. # get the Account link
  24. account_link = self.driver.find_element_by_link_text('ACCOUNT')
  25. # check My Account link is displayed/visible in the Home page footer
  26. self.assertTrue(account_link.is_displayed())
  27. def test_account_links(self):
  28. # get the all the links with Account text in it
  29. account_links = self.driver.\
  30. find_elements_by_partial_link_text('ACCOUNT')
  31. # check Account and My Account link is displayed/visible in the Home page footer
  32. self.assertTrue(len(account_links), 2)
  33. def test_count_of_promo_banners_images(self):
  34. # get promo banner list
  35. banner_list = self.driver.find_element_by_class_name('promos')
  36. # get images from the banner_list
  37. banners = banner_list.find_elements_by_tag_name('img')
  38. # check there are 3 banners displayed on the page
  39. self.assertEqual(3, len(banners))
  40. def test_vip_promo(self):
  41. # get vip promo image
  42. vip_promo = self.driver.\
  43. find_element_by_xpath("//img[@alt='Shop Private Sales - Members Only']")
  44. # check vip promo logo is displayed on home page
  45. self.assertTrue(vip_promo.is_displayed())
  46. # click on vip promo images to open the page
  47. vip_promo.click()
  48. # check page title
  49. self.assertEqual("VIP", self.driver.title)
  50. def test_shopping_cart_status(self):
  51. # check content of My Shopping Cart block on Home page
  52. # get the Shopping cart icon and click to open the Shopping Cart section
  53. shopping_cart_icon = self.driver.\
  54. find_element_by_css_selector('div.header-minicart span.icon')
  55. shopping_cart_icon.click()
  56. # get the shopping cart status
  57. shopping_cart_status = self.driver.\
  58. find_element_by_css_selector('p.empty').text
  59. self.assertEqual('You have no items in your shopping cart.',
  60. shopping_cart_status)
  61. # close the shopping cart section
  62. close_button = self.driver.\
  63. find_element_by_css_selector('div.minicart-wrapper a.close')
  64. close_button.click()
  65. @classmethod
  66. def tearDownClass(cls):
  67. # close the browser window
  68. cls.driver.quit()
  69. if __name__ == '__main__':
  70. 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:查找元素的更多相关文章

  1. 【转】使用Python学习selenium测试工具

    出处:https://my.oschina.net/u/1433482/blog/633231?fromerr=vaxqh9bn

  2. Python学习--Selenium模块学习(2)

    Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...

  3. Python中的测试工具

      当我们在写程序的时候,我们需要通过测试来验证程序是否出错或者存在问题,但是,编写大量的测试来确保程序的每个细节都没问题会显得很繁琐.在Python中,我们可以借助一些标准模块来帮助我们自动完成测试 ...

  4. Python学习--Selenium模块

    1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台

  5. 基于Python的XSS测试工具XSStrike使用方法

    基于Python的XSS测试工具XSStrike使用方法 简介 XSStrike 是一款用于探测并利用XSS漏洞的脚本 XSStrike目前所提供的产品特性: 对参数进行模糊测试之后构建合适的payl ...

  6. Python学习--Selenium模块简单介绍(1)

    简介及运行流程 Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozi ...

  7. 【python驱动】python进行selenium测试时GeckoDriver放在什么地方?

    背景:用python进行selenium 关于b/s架构的测试,需要配置驱动否则程序无法执行 情况1:windows下放置GeckoDriver 步骤1:下载驱动 GeckoDriver下载地址fir ...

  8. python:selenium测试登录在chrome中闪退

    问题描述:使用selenium.webdriver时测试网页,进行自动登录测试总是在登录成功时闪退.使用指定驱动器位置的方式chrome也会闪退 1.正常使用chrome驱动打开一个网页,正常访问 f ...

  9. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...

随机推荐

  1. Protocol Buffers简明教程

    随着微服务架构的流行,RPC框架渐渐地成为服务框架的一个重要部分. 在很多RPC的设计中,都采用了高性能的编解码技术,Protocol Buffers就属于其中的佼佼者. Protocol Buffe ...

  2. asp.net core 2.0 api ajax跨域问题

    API配置: services.AddCors(options => { options.AddPolicy("any", builder => { builder.W ...

  3. [CQOI2015]任务查询系统(未完成)

    链接:https://www.luogu.org/problemnew/show/P3168 题意:有n个任务,在si-ti发生,有优先级pi,求x时刻前k个pi之和 n,si,ti<=1000 ...

  4. openstack基础环境准备(一)

    一.环境介绍 操作系统 ip地址 主机名 服务 centos7.5 192.168.56.11 linux-node1 控制节点 centos7.5 192.168.56.12 linux-node2 ...

  5. 078 Hbase中rowkey设计原则

    1.热点问题 在某一时间段,有大量的数据同时对一个region进行操作 2.原因 对rowkey的设计不合理 对rowkey的划分不合理 3.解决方式 rowkey是hbase的读写唯一标识 最大长度 ...

  6. js类型判断-丰富加好用

    一, 自己有时候写一些东西,要做类型判断,还有测试的时候,对于原生的和jQuery中的类型判断,实在不敢恭维,所以就写了一个好用的类型判断,一般情况都够用的. function test(type) ...

  7. Python:安装MYSQL Connector

    在Python中安装MySQL Connector有如下三种方法: 1.直接安装客户端[建议使用] pip install mysqlclient 2.安装mysql连接器 pip install - ...

  8. node的模块包装为promise写法

    1. const Promise = require('bluebird') const fs = Promise.promisifyAll(Promise.promisify(require('fs ...

  9. js监听全屏的事件

    window.addEventListener('click',function(){ window.top.location.href = '../../loginOut.do';  //解决ifr ...

  10. java中的文件下载

    package com.pb.down;import java.io.File;import java.io.FileInputStream;import java.io.IOException; i ...