page_objects库利用PO设计模式的思想,对元素定位进行了封装,代码非常简洁,可以直接使用;
page_objects中的PageElement元素定位保方式变量如下:
css:css
id_:id
name:name
xpath:xpath
link_text:link_text
partial_link_text
tag_name
class_name
举例:
search_box = PageElement(id_="kw")
search_box = PageElement(name_="kw")
search_box = PageElement(xpath="//*[@id=''kw]")
一、安装方式
pip install page_objects
二、使用方法
#先定义基础baidu_page.py文件:
from page_objects import PageElement, PageObject#引入库
class BaiduPage(PageObject):
search_box = PageElement(id_="kw")
search_button = PageElement(id_="su")
#引用baidu_page.py文件,并利用unittest框架进行用例编写
from baidu_page1 import BaiduPage
import unittest
from selenium import webdriver
class BaiduSearch(unittest.TestCase):
@classmethod
def setUpClass(cls) :
cls.url = "https://www.baidu.com"
cls.driver = webdriver.Chrome()
@classmethod
def tearDownClass(cls) -> None:
cls.driver.quit()
def test_case_01(self):
baidu_search = BaiduPage(self.driver)
baidu_search.get(self.url)#调用get方法
baidu_search.search_box.send_keys("python")#调用search_box变量传入搜索值
baidu_search.search_button.click()
def test_case_01(self):
baidu_search = BaiduPage(self.driver)
baidu_search.get(self.url)
baidu_search.search_box.send_keys("selenium")
baidu_search.search_button.click()
if __name__ == 'main':
unittest.main()