架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 79|回复: 2

[交流] 【Playwright】(一)Playwright for .NET 使用教程

[复制链接]
发表于 4 天前 | 显示全部楼层 |阅读模式
Playwright

Playwright 是一个用于 Web 测试和自动化的框架。它允许使用单一 API 测试Chromium、Firefox和WebKit。Playwright旨在实现持久、强大、可靠且快速的跨浏览器 Web 自动化。

官网:https://playwright.dev/
源码:https://github.com/microsoft/playwright
Playwright for .NET 文档:https://playwright.dev/dotnet/docs/intro

  • 跨浏览器:Playwright支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。
  • 跨平台:可在 Windows、Linux 和 macOS 上进行测试,本地或持续集成 (CI),无头或有头。
  • 跨语言:在TypeScript、JavaScript、Python、.NET、Java中使用 Playwright API 。
  • 测试移动网页:Android版 Google Chrome 和移动版 Safari 的原生移动模拟。相同的渲染引擎可在您的桌面和云端运行。


运行环境

Playwright 以 .NET Standard 2.0 库的形式发布。我们推荐使用 .NET 8
Windows 10+、Windows Server 2016+ 或 Windows Subsystem for Linux (WSL)。
macOS 14 Ventura 或更高版本。
Debian 12、Ubuntu 22.04、Ubuntu 24.04,基于 x86-64 和 arm64 架构。

新建 .NET 8 控制台

首先,引入 nuget 包如下:

然后下载浏览器内核,Node.js 方式下载安装命令如下:

如下:
╔═══════════════════════════════════════════════════════════════════════════════╗
║ WARNING: It looks like you are running 'npx playwright install' without first ║
║ installing your project's dependencies.                                       ║
║                                                                               ║
║ To avoid unexpected behavior, please install your dependencies first, and     ║
║ then run Playwright's install command:                                        ║
║                                                                               ║
║     npm install                                                               ║
║     npx playwright install                                                    ║
║                                                                               ║
║ If your project does not yet depend on Playwright, first install the          ║
║ applicable npm package(most commonly @playwright/test), and                  ║
║ then run Playwright's install command to download the browsers:               ║
║                                                                               ║
║     npm install @playwright/test                                              ║
║     npx playwright install                                                    ║
║                                                                               ║
╚═══════════════════════════════════════════════════════════════════════════════╝
Downloading Chromium 136.0.7103.25 (playwright build v1169) from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1169/chromium-win64.zip
144.4 MiB[====================] 100% 0.0s
Chromium 136.0.7103.25 (playwright build v1169) downloaded to C:\Users\itsvse\AppData\Local\ms-playwright\chromium-1169
Downloading Chromium Headless Shell 136.0.7103.25 (playwright build v1169) from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1169/chromium-headless-shell-win64.zip
89.1 MiB[====================] 100% 0.0s
Chromium Headless Shell 136.0.7103.25 (playwright build v1169) downloaded to C:\Users\itsvse\AppData\Local\ms-playwright\chromium_headless_shell-1169
Downloading Firefox 137.0 (playwright build v1482) from https://cdn.playwright.dev/dbazure/download/playwright/builds/firefox/1482/firefox-win64.zip
92.1 MiB [====================] 100% 0.0s
Firefox 137.0 (playwright build v1482) downloaded to C:\Users\itsvse\AppData\Local\ms-playwright\firefox-1482
Downloading Webkit 18.4 (playwright build v2158) from https://cdn.playwright.dev/dbazure/download/playwright/builds/webkit/2158/webkit-win64.zip
57.1 MiB [====================] 100% 0.0s
Webkit 18.4 (playwright build v2158) downloaded to C:\Users\itsvse\AppData\Local\ms-playwright\webkit-2158
Downloading FFMPEG playwright build v1011 from https://cdn.playwright.dev/dbazure/download/playwright/builds/ffmpeg/1011/ffmpeg-win64.zip
1.3 MiB [====================] 100% 0.0s
FFMPEG playwright build v1011 downloaded to C:\Users\itsvse\AppData\Local\ms-playwright\ffmpeg-1011
Downloading Winldd playwright build v1007 from https://cdn.playwright.dev/dbazure/download/playwright/builds/winldd/1007/winldd-win64.zip
0.1 MiB [====================] 100% 0.0s
Winldd playwright build v1007 downloaded to C:\Users\itsvse\AppData\Local\ms-playwright\winldd-1007

或者使用 C# 在运行的时候自动检测进行安装,代码如下:

否则运行的时候,会报错如下:
Executable doesn't exist at C:\Users\itsvse\AppData\Local\ms-playwright\chromium-1169\chrome-win\chrome.exe
╔════════════════════════════════════════════════════════════╗
║ Looks like Playwright was just installed or updated.       ║
║ Please run the following command to download new browsers: ║
║                                                            ║
║     pwsh bin/Debug/netX/playwright.ps1 install             ║
║                                                            ║
║ <3 Playwright Team                                         ║
╚════════════════════════════════════════════════════════════╝

模拟输入用户名和密码,c# 代码如下:
效果图如下:

autoweb.gif

(完)




上一篇:B 站开源项目 IndexTTS 部署教程
下一篇:【Playwright】(二)常用功能和操作
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 4 天前 | 显示全部楼层
删除 Selenium、Playwright 和 Puppeteer 中的 WebDriver 自动化标志

测试:

QQ截图20250526170756.jpg
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 3 天前 | 显示全部楼层
点击按钮,等待 Ajax 加载完成后,再依次进行之后操作,代码如下:

码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-5-30 00:27

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表