一、安装
所有安装严格按照指定版本,不然可能会报错,为啥报错我也不知道
1、准备环境 win10(playwright好像不支持win7 python2)
2、 安装python3.7.9(这个是为了兼容robot)
https://www.python.org/downloads/windows/ 下载3.7.9版本
2、在线安装playwright 1.9.0版本
不要安装新版本,运行from playwright.sync_api import sync_playwright老报错,安装低版本解决了,
Traceback (most recent call last):
File "C:\aisa_ui\test.py", line 2, in <module>
from playwright.sync_api import sync_playwright
File "C:\Users\admin-pc1102\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright\sync_api\__init__.py", line 23, in <module>
import playwright.sync_api._generated
File "C:\Users\admin-pc1102\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright\sync_api\_generated.py", line 25, in <module>
from playwright._impl._accessibility import Accessibility as AccessibilityImpl
File "C:\Users\admin-pc1102\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright\_impl\_accessibility.py", line 17, in <module>
from playwright._impl._connection import Channel
File "C:\Users\admin-pc1102\AppData\Local\Programs\Python\Python311\Lib\site-packages\playwright\_impl\_connection.py", line 21, in <module>
from greenlet import greenlet
File "C:\Users\admin-pc1102\AppData\Local\Programs\Python\Python311\Lib\site-packages\greenlet\__init__.py", line 29, in <module>
from ._greenlet import _C_API # pylint:disable=no-name-in-module
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing _greenlet: 找不到指定的模块。
Process finished with exit code 1
pip install playwright==1.9.0 #安装playwright
playwright install #安装playwright支持的浏览器
pip uninstall playwright #如果装错了 可以卸载再执行上面命令重装
3、执行下面命令 能执行成功就代表安装没问题了
playwright codegen --target python -o open_baidu.py -b chromium https://www.baidu.com
4、python3环境下的robot安装
pip install robotframework
pip install robotframework-ride(不支持python3.8及以上版本,3.8及以上安装会报wxpython错误)
5、git安装
直接下一步安装就行,下载地址:
Git - Downloads
二、使用
录制脚本
1、python -m playwright codegen -b chromium --target python -o test.py
生成的test.py在重新运行时遇到了问题,如上,降低playwright版本后解决
2、打开https链接报错
使用context = browser.new_context(ignore_https_errors=True) 解决
三、元素定位:
文本定位:page.click("text=攻击成功告警数") 或者page.click("text=攻击成功告警数")
按钮定位:page.click("button(也可以是span等):has-text(\"登 录\")") #按钮包含登录字样
定位元素个数:a = page.locator("text="+text).count()
四、页面操作:
page.click(定位) #点击
page.fill(定位,值) #输入
page.text_content(定位) #获取文本值
page.press(定位,按键(比如Enter))
5、jenkins集成
遇到问题:
1、报错ms-playwright路径不对
解决:直接将pc上的ms-playwright文件夹复制到这个路径运行
2、邮件发送失败报错
Not sending mail to unregistered user XX because your SCM claimed this was associated with a user ID ‘XX' which your security realm does not recognize; you may need changes in your SCM plugin