硒界面
anna-lib的Python项目详细描述
安娜·利布
这个包的目的是简化硒的使用。
要求
安装
$ pip install anna-lib
使用量
fromanna_lib.seleniumimportdriver,events,assertionsresult=[]firefox=driver.create(driver='firefox',headless=True)firefox.get('http://example.com/')events.click(driver=firefox,target='a[href="http://www.iana.org/domains/example"]')result.append(assertions.current_url_is(firefox,'http://www.iana.org/domains/example'))
驱动器
使用此模块可基于一组选项创建Webdriver:
param | type | required | values | default value |
---|---|---|---|---|
driver | string | yes | 'firefox' or 'chrome' for now | 'firefox' |
headless | bool | no | True or False | False |
resolution | tuple | no | (width, height) | (1920, 1080) |
事件
使用此模块与页面交互。每个事件都需要一个驱动程序,一个默认为16秒的目标超时,但也需要一个值的send_keys
除外。
目标被视为css选择器,除非它以'$xpath'
开头,在这种情况下,它当然被视为xpath选择器。
fromanna_lib.seleniumimportevents,driverfirefox=driver.create('firefox',headless=True)events.click(driver=firefox,target='#search')events.send_keys(driver=firefox,target='#search',value='search terms')events.submit(driver=firefox,target='#search')events.hover(driver=firefox,target='$xpath//div.result/a')events.scroll_to(driver=firefox,target='#thing')events.switch_to(driver=firefox,target='iframe')
断言
使用此模块检查页的状态,无论是通过url还是通过页的元素。 每个断言都需要一个驱动程序,一些输入,一个默认为16秒的超时参数。
fromanna_lib.seleniumimportassertions,driverfirefox=driver.create('firefox',headless=True)try:assertions.url_equals(driver=firefox,expected='about:blank')assertions.in_url(driver=firefox,part='blank')assertions.element_exists(driver=firefox,target='body')exceptValueErrorase:print(str(e))exceptTypeErrorase:print(str(e))