网站首页lua/js
python使用playwrite前端自动化测试
发布时间:2024-08-26 12:55:25编辑:阅读(297)
1. 安装playwrite
pip install playwright
2. 安装支持的浏览器驱动
# 安装支持的浏览器:cr, chromium, ff, firefox, wk 和 webkit
playwright install chromium # 安装指定的chromium浏览器
3. 使用python脚本启动浏览器并获取网页内容
from playwright.sync_api import sync_playwright
def run():
with sync_playwright() as p:
#启动浏览器
browser = p.chromium.launch(headless=False, proxy={
"server": "http://proxy.com:9845",
# 需要身份验证,可以通过这里指定用户名和密码
'username': 'your-username',
'password': 'your-password'
})
page = browser.new_page()
#等待加载完整页面
url = f"https://www.website.com"
page.goto(url)
page.wait_for_load_state('domcontentloaded')
page.wait_for_timeout(4000)
#获取网页源码并验证是否有效
content = page.content()
if "Please verify yourself to continue" in content:
input("出现验证------------------------")
#保存网页源码
filename = "source.html"
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
#保存网页截图
page.screenshot(path='screenshot.png')
#关闭浏览器
browser.close()
if __name__ == "__main__":
run()
评论