自动化测试框架:DrissionPage 如果只要控制浏览器,导入ChromiumPage: 如果只要收发数据包,导入SessionPage: WebPage是功能最全面的页面类,既可控制浏览器,也可收发数据包。 常用 1、定位元素 2、爬取网页 3.下载网页 4.元素查找 5.元素操作 6.获取元素属性
如果只要控制浏览器,导入ChromiumPage:
from DrissionPage import ChromiumPage
如果只要收发数据包,导入SessionPage:
from DrissionPage import SessionPage
WebPage是功能最全面的页面类,既可控制浏览器,也可收发数据包。
from DrissionPage import WebPage
常用
1、定位元素
from DrissionPage import ChromiumPage
page = ChromiumPage( )
page. get( 'https://gitee.com/login' )
ele = page. ele( '#user_login' )
ele. input ( '您的账号' )
page. ele( '#user_password' ) . input ( '您的密码' )
page. ele( '@value=登 录' ) . click( )
2、爬取网页
from DrissionPage import SessionPage
page = SessionPage( )
for i in range ( 1 , 4 ) : page. get( f'https://gitee.com/explore/all?page= { i} ' ) links = page. eles( '.title project-namespace-path' ) for link in links: print ( link. text, link. link)
3.下载网页
from DrissionPage import SessionPage url = 'https://www.baidu.com/img/flexible/logo/pc/result.png'
save_path = r'C:\download' page = SessionPage( )
page. download( url, save_path, 'img' )
4.元素查找
page. ele( '@id:ele_id' , timeout= 2 )
page. eles( '@class' )
page. eles( '@class:class_name' )
page. eles( '@class=class_name' )
page. ele( '#ele_id' )
page. ele( '#:ele_id' )
page. ele( '.ele_class' )
page. ele( '.:ele_class' )
page. ele( 'tag:li' )
page. eles( 'tag:li' )
page. ele( 'tag:div@class=div_class' )
page. ele( 'tag:div@class:ele_class' )
page. ele( 'tag:div@class=ele_class' )
page. ele( 'tag:div@text():search_text' )
page. ele( 'tag:div@text()=search_text' )
page. ele( 'search text' )
page. eles( 'text:search text' )
page. eles( 'text=search text' )
page. eles( 'xpath://div[@class="ele_class"]' )
page. eles( 'css:div.ele_class' )
loc1 = By. ID, 'ele_id'
loc2 = By. XPATH, '//div[@class="ele_class"]'
page. ele( loc1)
page. ele( loc2)
element = page. ele( '@id:ele_id' )
element. ele( '@class:class_name' )
element. eles( 'tag:li' )
element. parent
element. next
element. prev
ele1 = element. shadow_root. ele( 'tag:div' )
page. ele( '@id:ele_id' ) . ele( 'tag:div' ) . next . ele( 'some text' ) . eles( 'tag:a' )
eles = page( '@id:ele_id' ) ( 'tag:div' ) . next ( 'some text' ) . eles( 'tag:a' )
ele2 = ele1( 'tag:li' ) . next ( 'some text' )
5.元素操作
element. click( by_js)
element. input ( value)
element. run_script( js)
element. submit( )
element. clear( )
element. screenshot( path, filename)
element. select( text)
element. set_attr( attr, value)
element. remove_attr( attr)
element. drag( x, y, speed, shake)
element. drag_to( ele_or_loc, speed, shake)
element. hover( )
6.获取元素属性
element. html
element. inner_html
element. tag
element. text
element. comments
element. link
element. texts( )
element. attrs
element. attr( attr)
element. css_path
element. xpath
element. parent
element. next
element. prev
element. parents( num)
element. nexts( num, mode)
element. prevs( num, mode)
element. ele( loc_or_str, timeout)
element. eles( loc_or_str, timeout)