Selenium Python教程第6章:使用页面对象

在这里插入图片描述

6. Page Objects 页面对象

6.1 什么是页面对象模型(POM)?

页面对象模型(Page Objects Model, POM )是一组旨在表示一个或多个网页的类, 用1个类来保存1个网页上所有的元素,相似的网页可以重用此类。
1个网站通常有多个页面,可以用多个页面类对象分别代表各个页面,其好处有:

  • web应用测试程序或者爬虫程序的结构更加清晰易懂。
  • 对于结构相似的多个网页,可减少重复的代码量
  • 如果web页面元素发生变化,只需要修改一处

6.2 使用页面对象的项目结构

使用页面对象的项目的通常结构类似于

 |-- pages
    |--- locators.py
    |--- elements.py
    |--- pages.py
|-- tests
    |--- test_contact_page.py

各文件说明:

  • pages.py 定义页面元素,以及针对各元素的操作方法
  • locators.py 分离定位字符。通常做法,同一页面的定位器属于同一个类
  • elements.py 通常定义1个页面元素的基类,提供set(), get()方法
  • test_*.py 测试用例类test case class文件

6.2 源码文件及说明

6.2.1 测试用例类源码

本例 test_contact_page.py 用于在colibri-software.com网站填写 Contact Me表单,判断是否填写结果是否成功

import unittest
from selenium import webdriver
import pages

class TestColibriSoftwareContactMe(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("https://www.colibri-software.com")

    def test_submit_contact_me_form(self):
        """
         "Contact me" 测试表单
        填写各字段,提交表单,验证提交是否成功
        """

        # 加载主页,本例为 colibri-software.com主页
        contact_page = pages.ContactPage(self.driver)

        # Checks if the word "Contact" is in title
        assert contact_page.is_title_matches()

        # 向表单各字段填写内容
        contact_page.name_input = 'John Doe'
        contact_page.company_name_input = 'John Doe\'s paper company'
        contact_page.email_input = 'jdoe@gmail.com'
        contact_page.additional_info_input = 'I need a website to sell paper online'

        # 提前表单
        contact_page.click_submit_form_button()

        # 验结果是否成功
        assert contact_page.success_message_is_displayed()

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

6.2.2 页面对象类源码

pages.py 介绍如何编写页面对象类

from elements import BasePageElement
from locators import ContactPageLocators

class NameElement(BasePageElement):
    """
    This class gets the search text from the specified locator
    """

    # The locator for text box where name is entered
    locator = 'wpforms-236-field_0'

# Similar classes for other text fields    
class CompanyNameElement(BasePageElement):

    locator = 'wpforms-236-field_4'

class EmailElement(BasePageElement):

    locator = 'wpforms-236-field_1'

class AdditionalInfoElement(BasePageElement):

    locator = 'wpforms-236-field_0'

class BasePage(object):
    """
    Base class to initialize the base page that will be called from all pages
    """

    def __init__(self, driver):
        self.driver = driver

class ContactPage(BasePage):
    """
    Contact page action methods come here
    """

    # Declares text input fields
    name_input = NameElement()
    company_name_input = CompanyNameElement()
    email_input = EmailElement()
    additional_info_input = AdditionalInfoElement()

    def is_title_matches(self):
        """
        Verifies that the text "Contact" appears in page title
        """
        return 'Contact' in self.driver.title

    def click_submit_form_button(self):
        """
        Submits the form
        """
        element = self.driver.find_element(*ContactPageLocators.SUBMIT_FORM_BUTTON)
        element.click()

    def success_message_is_displayed(self):
        success_message = 'Thanks for contacting us! We will be in touch with you shortly.'
        return success_message in self.driver.page_source

6.2.3 页面元素基类源码

elements.py 定义1个页面元素的基类,提供set(), get()方法

from selenium.webdriver.support.ui import WebDriverWait

class BasePageElement(object):
    """
    Base page class that is initialized on every page object class.
    """

    def __set__(self, obj, value):
        """
        Sets the text to the value supplied
        """
        driver = obj.driver
        WebDriverWait(driver, 100).until(lambda driver: driver.find_element_by_id(self.locator))
        driver.find_element_by_id(self.locator).clear()
        driver.find_element_by_id(self.locator).send_keys(value)

    def __get__(self, obj, owner):
        """
        Gets the text of the specified object
        """
        driver = obj.driver
        WebDriverWait(driver, 100).until(lambda driver: driver.find_element_by_id(self.locator))
        element = driver.find_element_by_id(self.locator)
        return element.get_attribute("value")

6.2.4 locators 定位器类
一个好的编程习惯做法是,分离定位字符。在这个例子中,同一页面的定位器属于同一个类。

from selenium.webdriver.common.by import By

class ContactPageLocators(object):
    """
    A class for all Contact page locators.
    """
    SUBMIT_FORM_BUTTON = (By.CSS_SELECTOR, 'button[type="submit"]')

class SearchResultsPageLocators(object):
    """A class for search results locators. All search results locators should come here"""
    pass
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Selenium爬取京东Python图书第一本书的信息的步骤: 1. 安装Selenium库和Chrome浏览器驱动。 2. 导入Selenium库和时间库: ```python from selenium import webdriver import time ``` 3. 创建浏览器对象并访问京东网站: ```python driver = webdriver.Chrome() driver.get('https://www.jd.com/') ``` 4. 找到搜索框并输入关键词“Python”。 ```python search_box = driver.find_element_by_id('key') search_box.send_keys('Python') ``` 5. 找到搜索按钮并点击。 ```python search_button = driver.find_element_by_class_name('button') search_button.click() ``` 6. 等待页面加载完成。 ```python time.sleep(5) ``` 7. 找到图书分类并点击。 ```python book_category = driver.find_element_by_xpath('//*[@id="J_cate"]/ul/li[1]/a') book_category.click() ``` 8. 等待页面加载完成。 ```python time.sleep(5) ``` 9. 找到第一本书的标题、价格和链接。 ```python book_title = driver.find_element_by_xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[3]/a/em') book_price = driver.find_element_by_xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[2]/strong/i') book_link = driver.find_element_by_xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[1]/a') ``` 10. 打印书籍信息。 ```python print('书名:', book_title.text) print('价格:', book_price.text) print('链接:', book_link.get_attribute('href')) ``` 11. 关闭浏览器。 ```python driver.quit() ``` 完整代码如下: ```python from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://www.jd.com/') search_box = driver.find_element_by_id('key') search_box.send_keys('Python') search_button = driver.find_element_by_class_name('button') search_button.click() time.sleep(5) book_category = driver.find_element_by_xpath('//*[@id="J_cate"]/ul/li[1]/a') book_category.click() time.sleep(5) book_title = driver.find_element_by_xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[3]/a/em') book_price = driver.find_element_by_xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[2]/strong/i') book_link = driver.find_element_by_xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[1]/a') print('书名:', book_title.text) print('价格:', book_price.text) print('链接:', book_link.get_attribute('href')) driver.quit() ``` 运行之后就可以爬取京东Python图书第一本书的信息了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值