您的位置:首页 > 编程语言 > Python开发

使用Python学习selenium测试工具-4:查找元素

2018-05-30 13:53 731 查看

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

实例

 

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: