playwright使用
基本安装和使用
Playwright 是一个用于自动化浏览器的强大工具,支持多种编程语言,包括Python。它可以用于Web应用程序的测试、抓取和自动化任务。以下是如何安装和使用Playwright的详细步骤以及示例代码。
安装Playwright
- 安装Playwright库
pip install playwright
- 安装浏览器
python -m playwright install
使用Playwright
以下是一个简单的示例,展示了如何使用Playwright打开一个网页并截取屏幕截图。
示例代码
解释
from playwright.sync_api import sync_playwright
def run(playwright):
# 启动浏览器
browser = playwright.chromium.launch(headless=False) # headless=True 表示无头模式
context = browser.new_context()
# 打开新页面
page = context.new_page()
# 导航到URL
page.goto("https://example.com")
# 截取屏幕截图
page.screenshot(path="example.png")
# 关闭浏览器
browser.close()
with sync_playwright() as playwright:
run(playwright)
详细解释
- 导入 Playwright 库
from playwright.sync_api import sync_playwright
- 定义运行函数
解释
def run(playwright):
# 启动浏览器(这里使用的是Chromium,可以替换为firefox或webkit)
browser = playwright.chromium.launch(headless=False) # headless=False 表示有头模式,headless=True 表示无头模式(后台运行)
# 创建新的浏览上下文(类似于无痕模式)
context = browser.new_context()
# 打开新页面
page = context.new_page()
# 导航到指定URL
page.goto("https://example.com")
# 截取屏幕截图并保存到本地文件系统
page.screenshot(path="example.png")
# 关闭浏览器
browser.close()
- 启动 Playwright 并运行
with sync_playwright() as playwright:
run(playwright)
更多示例
示例1:填写表单并提交
以下是一个示例,展示了如何使用Playwright填写表单并提交:
解释
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://example.com/form")
# 填写表单字段
page.fill('input[name="name"]', 'John Doe')
page.fill('input[name="email"]', 'john.doe@example.com')
# 点击提交按钮
page.click('button[type="submit"]')
browser.close()
with sync_playwright() as playwright:
run(playwright)
示例2:等待元素加载并获取文本内容
以下是一个示例,展示了如何使用PlayWright等待元素加载并获取文本内容:
from playWright.sync_api import sync_playWright
def run (playWright):
browser=playWright.chromium.launch(headless=False)
context=browser.new_context()
page=context.new_page()
page.goto("https://example.com")
# 等待元素加载
page.wait_for_selector('h1')
# 获取文本内容
text=page.text_content('h1')
print(f"页面标题: {text}")
browser.close()
with sync_playWright() as playWright:
run (playWright)
总结
通过上述步骤和示例代码,你可以在Python中使用PlayWright进行Web应用程序的自动化测试、抓取和其他任务。这些示例包括打开网页、截取屏幕截图、填写表单、等待元素加载以及获取文本内容等。
离线安装问题处理
如果在执行 python -m playwright install
时遇到安装失败的问题,可以尝试以下步骤来解决问题:
1. 确保Python和pip已正确安装
首先,确保你已经正确安装了Python和pip。可以通过以下命令检查它们的版本:
python --version
pip --version
2. 安装Playwright库
使用pip安装Playwright库:
pip install playwright
3. 安装浏览器
安装Playwright库后,需要安装浏览器。可以使用以下命令:
python -m playwright install
常见问题及解决方案
问题1:网络连接问题
如果在下载浏览器时遇到网络连接问题,可以尝试设置代理或使用离线安装包。
设置代理
如果你在公司网络或需要通过代理连接互联网,可以设置HTTP_PROXY和HTTPS_PROXY环境变量:
export HTTP_PROXY=http://your-proxy:port
export HTTPS_PROXY=https://your-proxy:port
使用离线安装包
你可以从Playwright官方GitHub仓库下载离线浏览器包,然后手动解压并配置。
-
下载离线浏览器包:
- 前往Playwright GitHub Releases页面,找到对应版本的离线包并下载。
-
解压缩文件并配置Playwright使用本地浏览器:
- 将下载的压缩包解压到某个目录,例如
/path/to/browsers
。 - 设置PLAYWRIGHT_BROWSERS_PATH环境变量指向该目录:
export PLAYWRIGHT_BROWSERS_PATH=/path/to/browsers
- 将下载的压缩包解压到某个目录,例如
问题2:权限问题
如果在安装过程中遇到权限问题,可以尝试使用sudo命令(仅限Linux/macOS)或以管理员身份运行命令(Windows)。
Linux/macOS
sudo python -m playwright install
Windows
以管理员身份打开命令提示符,然后运行以下命令:
python -m playwright install
问题3:依赖项缺失
有时,缺少系统依赖项也会导致安装失败。可以尝试安装一些常见的依赖项。
Debian/Ubuntu
sudo apt-get update && sudo apt-get install -y libatk1.0-0 libatk-bridge2.0-0 libcups2 libxkbcommon-x11-0 libgbm-dev
CentOS/RHEL
sudo yum install -y atk cups-libs libXcomposite libXcursor libXdamage libXext libXi libXrandr libXScrnSaver libXtst pango alsa-lib gtk3 mesa-libEGL mesa-libgbm xorg-x11-server-Xvfb wget bzip2 fontconfig freetype glib2 glibc zlib nss nspr alsa-lib at-spi2-core dbus-glib dbus-libs expat gdk-pixbuf2 glib-networking gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme json-glib librsvg2 pango polkit shared-mime-info xdg-utils zenity xorg-x11-fonts-Type1 xorg-x11-fonts-misc xorg-x11-utils xorg-x11-xinit-session xorg-x11-server-Xephyr xorg-x11-server-Xnest xorg-x11-server-Xspice xorg-x11-server-Xwayland xorg-x11-server-common xorg-x11-server-devel xorg-x11-server-source xorg-x11-twm xterm xvfb-run nss-mdns avahi-daemon avahi-libs avahi-tools avahi-ui-tools avahi-dnsconfd avahi-discover avahi-dispatcher avahi-utils avahi-autoipd avahi-browse-avahi-dnsconfd gtk3-devel gtk3-engines gtk3-immodules gtk3-immodule-xim gtk3-inspector gtk3-nocsd gtk3-print-backends gtk3-theme-adwaita gnome-themes-standard gnome-themes-extra gnome-themes-extra-symbolic gnome-themes-extras gnome-themes-extras-symbolic adwaita-gtk3-theme adwaita-icon-theme adwaita-icon-theme-symbolic adwaita-qt5 adwaita-qt6 adwaita-shell-extension adwaita-shell-extension-common adwaita-shell-extension-devel adwaita-shell-extension-docs adwaita-shell-extension-examples adwaita-shell-extension-tests adwaita-shell-extension-tutorials adwaita-shell-extras adwaita-shell-extras-common adwaita-shell-extras-devel adwaita-shell-extras-docs adwaita-shell-extras-examples adwaita-shell-extras-tests adwaita-shell-extras-tutorials adwaita-icons-common adwaita-icons-devel adwaita-icons-docs adwaiticons-symbolic-common nspr-devel nspr-docs nspr-tools nspr-utils alsamixergui alsamixer alsaplayer alsaplayer-daemon alsaplayer-devel alsaplayer-docs alsaplayer-esd alsaplayer-freeworld alsaplayer-gstreamer alsaplayer-jack alsaplayer-ladspa alsaplayer-nas alsaplayer-oss alsaplayer-plug-ins alsaplayer-sidplay alsaplayer-skinning-toolkit dbus-glib-devel dbus-glib-docs dbus-glib-tools dbus-glib-utils dbus-glib-sharp dbus-glib-sharp-devel dbus-glib-sharp-docs dbus-glib-sharp-tools dbus-glib-sharp-utils dbus-libs-devel dbus-libs-docs dbus-libs-tools dbus-libs-utils expat-devel expat-docs expat-tools expat-utils expat-sharp expat-sharp-devel expat-sharp-docs expat-sharp-tools expat-sharp-utils gdk-pixbuf2-devel gdk-pixbuf2-docs gdk-pixbuf2-tools gdk-pixbuf2-utils json-glib-devel json-glib-docs json-glib-tools json-glib-utils librsvg2-devel librsvg2-docs librsvg2-tools librsvg2-utils pango-devel pango-docs pango-tools pango-utils polkit-devel polkit-docs polkit-tools polkit-utils shared-mime-info-devel shared-mime-info-docs shared-mime-info-tools shared-mime-info-utils xdg-utils-devel zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve zenity-deve
离线手动解决方案参考
现象一:下载浏览器失败
python -m playwright install
Downloading Chromium ***(playwright build v1067) from https://playwright.azureedge.net/builds/chromium/1067/chromium-win64.zip
Error: connect ETIMEDOUT ***
解决方案:手工下载,后解压
https://playwright.azureedge.net/builds/chromium/1067/chromium-win64.zip
现象二:编写好脚本,执行时提示没有浏览器。
playwright._impl._api_types.Error: Executable doesn't exist at C:\Users\...\AppData\Local\ms-playwright\chromium-1067\chrome-win\chrome.exe
解决方案:把上1个方案的“chromium-win64.zip”解压到对应路径下。
" C:\Users\...\AppData\Local\ms-playwright\chromium-1067\chrome-win\chrome.exe"
...是当前登录的路径下。
总结
通过上述步骤,你可以解决在执行python -m playwright install时遇到的问题。这些步骤包括确保Python和pip已正确安装、设置代理、手动下载离线浏览器包以及检查系统依赖项等。
参考文档
https://blog.csdn.net/HAMYHF/article/details/143329066