Python的页面对象模型DSL,灵感来自rubysiteprism

pyte-prism的Python项目详细描述


灵感来自Ruby siteprishttps://github.com/site-prism/site_prism

概要

以下是PytePrism的设计使用概述:

fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.common.keysimportKeysfrompyte_prismimportPage,PageSection,Element,Section,SectionsclassMenu(PageSection):gmail=Element(By.XPATH,"//a[text()='Gmail']")images=Element(By.XPATH,"//a[text()='Images']")classSearchResult(PageSection):title=Element(By.CSS_SELECTOR,"div.r > a > h3")title_link=Element(By.CSS_SELECTOR,"div.r > a")blurb=Element(By.CSS_SELECTOR,"div.s > div > span")classHome(Page):_url="https://www.google.com/"search_field=Element(By.CSS_SELECTOR,"input[name='q'")search_button=Element(By.CSS_SELECTOR,"div.FPdoLc.tfB0Bf input[name='btnK']")footer_links=Element(By.CSS_SELECTOR,"#footer a")menu=Section(Menu,By.CSS_SELECTOR,"#gbw")classSearchResults(Page):_url_matcher="https://www.google.com/results\?.*"menu=Section(Menu,By.CSS_SELECTOR,"#gbw")search_results=Sections(SearchResult,By.CSS_SELECTOR,"#search > div > div > div.g")defsearch_result_links(self):return[result.title.get_attribute('href')forresultinself.search_results]defmain():driver=webdriver.Chrome()home=Home(driver)home.load()home.wait_until_menu_visible()# menu loads after a second or 2, give it time to arriveprint(home.has_menu())#=> Trueprint(home.has_search_field())#=> Trueprint(home.has_search_button())#=> Truehome.search_field.send_keys("Sausages")home.search_field.send_keys(Keys.ESCAPE)# Close suggestion boxhome.wait_until_search_button_visible()# Wait until suggestion box closehome.search_button.click()results_page=SearchResults(driver)results_page.is_loaded()#=> True ( Does _url_matcher matches current_url? )results_page.wait_until_search_results_visible(timeout=30)# default timeout is 10 secprint(len(results_page.search_results))#=> about 10titles=[result.title.textforresultinresults_page.search_results]links=[result.title_link.get_attribute('href')forresultinresults_page.search_results]descr=[result.blurb.textforresultinresults_page.search_results]frompprintimportpprintpprint(list(zip(titles,links,descr)))"""
  #=> [('Sausage - Wikipedia', 'https://en.wikipedia.org/wiki/Sausage', 'Sausages are ...'),
  #    ('List of sausages - Wikipedia', 'https://en.wikipedia.org/wiki/List_of_sausages', 'This is a lias of ...'),
  #    ...]
  """if__name__=="__main__":main()

要求

  • Python3

特点

  • 多平台支持:Linux、macOS、Windows
  • 纯Python

安装

通过Pypi和Python包管理器:

^{pr2}$

要从git存储库安装最新版本:

git clone https://github.com/fkshom/pyte-prism.git
cd pyte-prism
python setup.py install

示例

fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.common.keysimportKeysfrompyte_prismimportPage,PageSection,PageIframe,Element,Section,Sections,Iframe# Section must inherit PageSection classclassMySection(PageSection):title=Element(By.XPATH,"//div[@class='title']")# Iframe must inherit PageIframe classclassMyIframe(PageIframe):title=Element(By.XPATH,"//div[@class='title']")# Page must inherit Page classclassHome(Page):_url="http://example.com/"# _url is used by Page.load() method._url_matcher=r"https?://example.com/.*"# _url_matcher is used by Page.is_loaded() method.# definition of elementgo_button=Element(By.ID,"#button")keyword_box=Element(By.ID,"#keyword")items=Elements(By.CSS_SELECTOR,"ul#items > li")section=Setion(MySection,By.ID,"#section")sections=Sections(MySection,By.ID,"#sections")# MySection is not typomyiframe=IFrame(MyIframe,BY.ID,"#iframe")defmain():driver=webdriver.Chrome()home=Home(driver)home.load()# visit to "_url"home.wait_until_page_loaded()# check current_url is  _url or _url_matcher (if defined)home.assert_loaded()# raise Exception if is_loaded() is False.home.wait_until_page_readystate_is_complete(timeout=10)# wait until javascript readyState is complete.# Defined some supported methods automatically# home.wait_until_<variablename>_visible()          # None or Exception# home.wait_until_<variablename>_invisible()        # None or Exception# home.wait_until_<variablename>_to_be_clickable()  # None or Exception# home.has_<variablename>()                         # True or False# home.has_no_<variablename>()                      # True or Falsehome.keyword_box.send_keys('my keyword')# element is webdriver element.home.wait_until_go_button_visible(timeout=30)# timeout: default 10home.go_button.click()home.section.title.texthome.sections[0].title.text# swtich to, exit from iframe context automaticallywithhome.myiframeasiframe:iframe.title.text

参数化URL

PytePrism使用uritemplate模块,因此允许对url进行参数化。 见https://uritemplate.readthedocs.io/en/latest/

classHome(Page):_url='http://example.com/users{/userid}'_url_matcher='https?://example.com/users.*'classHome2(Page):_url='http://example.com/search{?keyword,lang}'_url_matcher='https?://example.com/search.*'defmain():driver=webdriver.Chrome()home=Home(driver)home.load()# visit to http://example.com/usershome.load(userid=100)# visit to http://example.com/users/100home2=Home2(driver)home2.load(keyword='mykeyword',lang='en')# visit to http://example.com/saearch?keyword=mykeyword&lang=en

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
使用php调用java web服务时无法获取输出   java Netbeans:安装IMlet时出错。。。。仿真器错误   初始化期间Eclipse Juno SR 2中的java堆栈溢出   java在3d中寻找两个任意立方体的交集   代码中显示java“org.openqa.selenium.WebDriverException”错误   java使用AtomicInteger作为可变整数的替代品是一种好的做法吗?   当以编程方式设置背景颜色时,java版面在更新后会自动复制   java将字符串拆分为一个带有数字数据的字符串和另一个带有非数字数据的字符串   带有SSL协议的java AsyncHttpClient   C++服务器HTTN到java NtoHS客户端转换   java内存分配是基于引用类还是实例类?   java使用ProjectLombok安全吗?   grails“java版本”在命令提示符下不工作   java无法使用axis2实例化类型ADBDataSource   在Play Framework 2中进行多文件上传的java示例   java如何将二进制补码字符串转换为负十进制数?   windows 7在Java中获取不正确的操作系统名称   java如何禁用p:commandButton的enter键?   java如何从main方法加载servlet   java如何从imageView获取二进制位图