卸载playwright
# 删除项目本地安装
rmdir /s /q "C:\Users\dwrli\node_modules\playwright"
# 删除全局安装
npm uninstall -g playwright
# 删除 npm 缓存
npm cache clean --force
# 删除临时文件
rmdir /s /q "%TEMP%\playwright*"
重新安装
# 创建新项目目录
mkdir C:\Users\dwrli\PlaywrightProjects
cd C:\Users\dwrli\PlaywrightProjects
# 初始化项目
npm init -y
# 安装最新版 Playwright
npm install playwright
下载Chromium并检测
npx playwright install chromium
#检测Chromium安装路径
npx playwright show-chromium-path
测试代码
const{chromium}=require('playwright'); //引入playwright的Chromium模块
console.log('实际加载的Chromium路径:', chromium.executablePath());
(async()=>{
//async-异步函数
//await-异步处理关键字,作用【暂停代码执行,直到完成某个操作】只能写在async函数中
//启动浏览器
const browser = await chromium.launch({
//显示浏览器窗口 headless:默认,无界面模式
headless: false,
});
//创建新页面
const page=await browser.newPage();
//访问网页(加载指定url)
await page.goto('Example Domain');
//截屏保存
await page.screenshot({path:'example.png'});
//关闭浏览器
//await browser.close();
})();