没有录屏,总结下两个现象:
1、在pycharm里执行 playwright codegen、cmd命令行里执行playwright codegen后,浏览器页面打开后会立马闪退
2、在pycharm里运行playwright代码,浏览器页面打开了,但是脚本那个窗口弹出来是空白的
甚至换了个新电脑都没解决,醉了。但找了一台虚拟机可以的。怀疑可能是win10版本的问题?不确定。
最终解决方案:
playwright降级,安装特定版本的,完美运行
pip install playwright==1.15.3
需要重新安装浏览器内核驱动
python -m playwright install
缺陷:
1.15.3版本的playwright,无法使用get_by_role、get_by_title等方法。因为这些在v1.18版本上才支持。但安装v1.18版本codegen就会闪退,绝了。
================================
在一台机器上安装2个版本的playwright
1、创建2个python虚拟环境
pip install virtualenv
python -m venv env1
python -m venv env2
2、激活虚拟环境
./env1/Scripts/activate.bat
./env2/Scripts/activate.bat
3、在每个激活的虚拟环境中,分别安装相应版本的playwright
pip install playwright==1.15.3
pip install playwright (安装最新版就行)
4、安装playwright依赖的浏览器驱动
先安装 node.js,参考 WIN10 安装npm及cnpm_windows10 npm_yahuikiki的博客-CSDN博客
在每个激活的虚拟环境中,在使用 playwright install 安装 Playwright 的时候指定版本,来区分不同的版本。
npx playwright@1.15.3 install
npx playwright@{步骤3中最新版本号} install
补充:
如果在运行 npx playwright@1.15.3 install
命令时出现 No matching version found for playwright@1.15.3.
的错误,这可能意味着指定的版本号不存在或被删除了。您可以通过以下方法解决这个问题:
-
检查 Playwright 的可用版本 请确保你正在调用当前已经发布的 Playwright 版本。使用
npm view playwright versions
命令列出可用的版本,确认需要安装的版本是否存在。 -
更新 npm 缓存 执行
npm cache clean --force
命令清除 npm 缓存,然后再次尝试安装所需的版本。 -
使用最新版本 考虑将
npx playwright@latest install
命令中的版本号替换为latest
,以安装最新版本的 Playwright。
如果上述步骤无法解决问题,请尝试从另一个镜像源下载。将默认的 NPM 镜像源更改为 taobao 应该会解决这个问题:
npm config set registry https://registry.npm.taobao.org/
然后再次运行 npx playwright@1.15.3 install
命令。