playwright安装及脚本录制
安装playwight
要求:python3.7+
安装命令:
pip install playwright
playwright install#安装chrome/firefox等浏览器的驱动⽂件
录制命令
playwright codegen csdn:打开csdn⽹站进⾏录制
playwright codegen --save-storage=auth.json csdn:录制结束时将cookies and localStorage 进⾏保存在auth.json⽂件中playwright codegen --load-storage=auth.json csdn:加载已保存的登录认证信息
playwright codegen -o test.py·:将录制的⽂件保存在test.py中
playwright codegen -b ff:打开firefox浏览器进⾏录制,默认是chrome,更多参数有: cr, chromium, ff, firefox, wk, webkit --device="iPhone 11" csdn:#模拟移动设备iPhone11,注意:device的值必须⽤双引号,并且区分⼤⼩写
--viewport-size = 800,600:设置窗⼝⼤⼩
元素定位
打开录制窗⼝,在console界⾯输⼊以下命名可以判断定位的元素是否准确
playwright.$(selector):出匹配的元素,到就返回
playwright.$$(selector):出所有匹配的元素
实际操作
控制台下输⼊playwright codegen www.csdn,进⾏输⼊软件测试进⾏搜索,录制的代码如下
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)input标签placeholder属性
context = w_context()
# Open new page
page = w_page()
# Go to www.csdn/
<("www.csdn/")
# Click [placeholder="git使⽤"] 录制的脚本使⽤的placeholder属性,但时此属性的值是每隔⼏秒就会改边,会导致脚本重放失败,所以改为以下⽅式定位
page.click("//input[@id='toolbar-search-input']")
# Fill [placeholder="git使⽤"]
page.fill("//input[@id='toolbar-search-input']","软件测试")
# Press Enter
# pect_navigation(url="so.csdn/so/search/all?q=%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95&t=all&p=1&s=0&tm=0&lv =-1&ft=0&l=&u="):
pect_navigation():
pect_popup()as popup_info:
page.press("//input[@id='toolbar-search-input']","Enter")
page1 = popup_info.value
# ---------------------
context.close()
browser.close()
with sync_playwright()as playwright:
run(playwright)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论