playwright在centos 7如何运行?

playwright在centos 7如何运行?

一、首先安装比较低的版本

# 安装playwright
pip install playwright==1.16.1 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

# 安装pytest-playwright
pip install pytest-playwright==0.2.2 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

# 安装playwright的浏览器驱动
playwright install

二、接着会碰到version `GLIBC_2.18’ not found的问题,参考https://deno.js.cn/t/topic/611解决

1.下载解压

wget https://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz
tar -zxvf glibc-2.18.tar.gz

2.编译安装

cd glibc-2.18 && mkdir build
cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make && make install

3.验证

[root@VM_0_7_centos build]# ll /lib64/libc.so.6
lrwxrwxrwx 1 root root 12 Mar 25 09:01 /lib64/libc.so.6 -> libc-2.18.so

若执行程序报错,内容如下:


Traceback (most recent call last):
browser = playwright.chromium.launch(headless=False)
File "/usr/local/python3/lib/python3.8/site-packages/playwright/sync_api/_generated.py", line 11164, in launch
self._sync(
File "/usr/local/python3/lib/python3.8/site-packages/playwright/_impl/_sync_base.py", line 111, in _sync
return task.result()
File "/usr/local/python3/lib/python3.8/site-packages/playwright/_impl/_browser_type.py", line 88, in launch
return from_channel(await self._channel.send("launch", params))
File "/usr/local/python3/lib/python3.8/site-packages/playwright/_impl/_connection.py", line 39, in send
return await self.inner_send(method, params, False)
File "/usr/local/python3/lib/python3.8/site-packages/playwright/_impl/_connection.py", line 63, in inner_send
result = next(iter(done)).result()
playwright._impl._api_types.Error: Browser closed.
==================== Browser output: ====================
<launching> /root/.cache/ms-playwright/chromium-930007/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-N45EXg --remote-debugging-pipe --no-startup-window
<launched> pid=21705
[pid=21705][err] [21705:21705:1006/002916.523519:FATAL:ozone_platform_x11.cc(238)] Check failed: x11::Connection::Get()->Ready(). Missing X server or $DISPLAY
[pid=21705][err] #0 0x55e847d04f99 base::debug::CollectStackTrace()
[pid=21705][err] #1 0x55e847c6f513 base::debug::StackTrace::StackTrace()
[pid=21705][err] #2 0x55e847c82f10 logging::LogMessage::~LogMessage()
[pid=21705][err] #3 0x55e847c83a5e logging::LogMessage::~LogMessage()
[pid=21705][err] #4 0x55e844f0aa29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
[pid=21705][err] #5 0x55e844ea8067 ui::OzonePlatform::InitializeForUI()
[pid=21705][err] #6 0x55e8493da507 aura::Env::Init()
[pid=21705][err] #7 0x55e8493da49a aura::Env::CreateInstance()
[pid=21705][err] #8 0x55e8458a409f content::BrowserMainLoop::InitializeToolkit()
[pid=21705][err] #9 0x55e8458a4a4d content::BrowserMainRunnerImpl::Initialize()
[pid=21705][err] #10 0x55e8458a0dfb content::BrowserMain()
[pid=21705][err] #11 0x55e84780c692 content::ContentMainRunnerImpl::RunBrowser()
[pid=21705][err] #12 0x55e84780c157 content::ContentMainRunnerImpl::Run()
[pid=21705][err] #13 0x55e8478099c5 content::RunContentProcess()
[pid=21705][err] #14 0x55e84780a49e content::ContentMain()
[pid=21705][err] #15 0x55e8445ca226 ChromeMain
[pid=21705][err] #16 0x7fb93dfc7ac5 __libc_start_main
[pid=21705][err] #17 0x55e8445ca02a _start
[pid=21705][err] Crash keys:
[pid=21705][err] "io_scheduler_async_stack" = "0x55E845686FE4 0x0"
[pid=21705][err] "variations" = "19ebe09a-4542122,23a898eb-fc93cf74,5f2c0f7c-3f4a17df,e4a357e9-3f4a17df,"
[pid=21705][err] "num-experiments" = "4"
[pid=21705][err] "switch-30" = "--enable-crashpad"
[pid=21705][err] "switch-29" = "--no-startup-window"
[pid=21705][err] "switch-28" = "--remote-debugging-pipe"
[pid=21705][err] "switch-27" = "--user-data-dir=/tmp/playwright_chromiumdev_profile-N45EXg"
[pid=21705][err] "switch-26" = "--no-sandbox"
[pid=21705][err] "switch-25" = "--no-service-autorun"
[pid=21705][err] "switch-24" = "--use-mock-keychain"
[pid=21705][err] "switch-23" = "--password-store=basic"
[pid=21705][err] "switch-22" = "--enable-automation"
[pid=21705][err] "switch-21" = "--no-first-run"
[pid=21705][err] "switch-20" = "--metrics-recording-only"
[pid=21705][err] "switch-19" = "--force-color-profile=srgb"
[pid=21705][err] "switch-18" = "--disable-sync"
[pid=21705][err] "switch-17" = "--disable-renderer-backgrounding"
[pid=21705][err] "switch-16" = "--disable-prompt-on-repost"
[pid=21705][err] "switch-15" = "--disable-popup-blocking"
[pid=21705][err] "switch-14" = "--disable-ipc-flooding-protection"
[pid=21705][err] "switch-13" = "--disable-hang-monitor"
[pid=21705][err] "switch-12" = "--allow-pre-commit-input"
[pid=21705][err] "switch-11" = "--disable-features=ImprovedCookieControls,LazyFrameLoading,Globa"
[pid=21705][err] "switch-10" = "--disable-extensions"
[pid=21705][err] "switch-9" = "--disable-dev-shm-usage"
[pid=21705][err] "switch-8" = "--disable-default-apps"
[pid=21705][err] "switch-7" = "--disable-component-extensions-with-background-pages"
[pid=21705][err] "switch-6" = "--disable-client-side-phishing-detection"
[pid=21705][err] "switch-5" = "--disable-breakpad"
[pid=21705][err] "switch-4" = "--disable-backgrounding-occluded-windows"
[pid=21705][err] "switch-3" = "--disable-background-timer-throttling"
[pid=21705][err] "switch-2" = "--enable-features=NetworkService,NetworkServiceInProcess"
[pid=21705][err] "switch-1" = "--disable-background-networking"
[pid=21705][err] "num-switches" = "30"
[pid=21705][err] "osarch" = "x86_64"
[pid=21705][err] "pid" = "21705"
[pid=21705][err] "ptype" = "browser"
[pid=21705][err]
[pid=21705][err] [1006/002916.644528:ERROR:process_memory_range.cc(86)] read out of range
[pid=21705][err] [1006/002916.644582:ERROR:elf_image_reader.cc(606)] missing nul-terminator
[pid=21705][err] [1006/002916.646240:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[pid=21705][err] [1006/002916.646256:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
[pid=21705][err] Received signal 6
[pid=21705][err] #0 0x55e847d04f99 base::debug::CollectStackTrace()
[pid=21705][err] #1 0x55e847c6f513 base::debug::StackTrace::StackTrace()
[pid=21705][err] #2 0x55e847d04a71 base::debug::(anonymous namespace)::StackDumpSignalHandler()
[pid=21705][err] #3 0x7fb94271f140 (/usr/lib64/libpthread-2.18.so+0x1013f)
[pid=21705][err] #4 0x7fb93dfdb2e7 __GI_raise
[pid=21705][err] #5 0x7fb93dfdc6c8 __GI_abort
[pid=21705][err] #6 0x55e847d03d05 base::debug::BreakDebuggerAsyncSafe()
[pid=21705][err] #7 0x55e847c8333f logging::LogMessage::~LogMessage()
[pid=21705][err] #8 0x55e847c83a5e logging::LogMessage::~LogMessage()
[pid=21705][err] #9 0x55e844f0aa29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
[pid=21705][err] #10 0x55e844ea8067 ui::OzonePlatform::InitializeForUI()
[pid=21705][err] #11 0x55e8493da507 aura::Env::Init()
[pid=21705][err] #12 0x55e8493da49a aura::Env::CreateInstance()
[pid=21705][err] #13 0x55e8458a409f content::BrowserMainLoop::InitializeToolkit()
[pid=21705][err] #14 0x55e8458a4a4d content::BrowserMainRunnerImpl::Initialize()
[pid=21705][err] #15 0x55e8458a0dfb content::BrowserMain()
[pid=21705][err] #16 0x55e84780c692 content::ContentMainRunnerImpl::RunBrowser()
[pid=21705][err] #17 0x55e84780c157 content::ContentMainRunnerImpl::Run()
[pid=21705][err] #18 0x55e8478099c5 content::RunContentProcess()
[pid=21705][err] #19 0x55e84780a49e content::ContentMain()
[pid=21705][err] #20 0x55e8445ca226 ChromeMain
[pid=21705][err] #21 0x7fb93dfc7ac5 __libc_start_main
[pid=21705][err] #22 0x55e8445ca02a _start
[pid=21705][err] r8: 0000000000000000 r9: 0000000000000a00 r10: 0000000000000008 r11: 0000000000000206
[pid=21705][err] r12: 00001d8600105400 r13: 00007ffd02024720 r14: 00001d8600105410 r15: aaaaaaaaaaaaaaaa
[pid=21705][err] di: 00000000000054c9 si: 00000000000054c9 bp: 00007ffd02024710 bx: 00007ffd02024f50
[pid=21705][err] dx: 0000000000000006 ax: 0000000000000000 cx: ffffffffffffffff sp: 00007ffd020245d8
[pid=21705][err] ip: 00007fb93dfdb2e7 efl: 0000000000000206 cgf: 0000000000000033 erf: 0000000000000000
[pid=21705][err] trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
[pid=21705][err] [end of stack trace]
=========================== logs ===========================
<launching> /root/.cache/ms-playwright/chromium-930007/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-N45EXg --remote-debugging-pipe --no-startup-window
<launched> pid=21705
[pid=21705][err] [21705:21705:1006/002916.523519:FATAL:ozone_platform_x11.cc(238)] Check failed: x11::Connection::Get()->Ready(). Missing X server or $DISPLAY
[pid=21705][err] #0 0x55e847d04f99 base::debug::CollectStackTrace()
[pid=21705][err] #1 0x55e847c6f513 base::debug::StackTrace::StackTrace()
[pid=21705][err] #2 0x55e847c82f10 logging::LogMessage::~LogMessage()
[pid=21705][err] #3 0x55e847c83a5e logging::LogMessage::~LogMessage()
[pid=21705][err] #4 0x55e844f0aa29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
[pid=21705][err] #5 0x55e844ea8067 ui::OzonePlatform::InitializeForUI()
[pid=21705][err] #6 0x55e8493da507 aura::Env::Init()
[pid=21705][err] #7 0x55e8493da49a aura::Env::CreateInstance()
[pid=21705][err] #8 0x55e8458a409f content::BrowserMainLoop::InitializeToolkit()
[pid=21705][err] #9 0x55e8458a4a4d content::BrowserMainRunnerImpl::Initialize()
[pid=21705][err] #10 0x55e8458a0dfb content::BrowserMain()
[pid=21705][err] #11 0x55e84780c692 content::ContentMainRunnerImpl::RunBrowser()
[pid=21705][err] #12 0x55e84780c157 content::ContentMainRunnerImpl::Run()
[pid=21705][err] #13 0x55e8478099c5 content::RunContentProcess()
[pid=21705][err] #14 0x55e84780a49e content::ContentMain()
[pid=21705][err] #15 0x55e8445ca226 ChromeMain
[pid=21705][err] #16 0x7fb93dfc7ac5 __libc_start_main
[pid=21705][err] #17 0x55e8445ca02a _start
[pid=21705][err] Crash keys:
[pid=21705][err] "io_scheduler_async_stack" = "0x55E845686FE4 0x0"
[pid=21705][err] "variations" = "19ebe09a-4542122,23a898eb-fc93cf74,5f2c0f7c-3f4a17df,e4a357e9-3f4a17df,"
[pid=21705][err] "num-experiments" = "4"
[pid=21705][err] "switch-30" = "--enable-crashpad"
[pid=21705][err] "switch-29" = "--no-startup-window"
[pid=21705][err] "switch-28" = "--remote-debugging-pipe"
[pid=21705][err] "switch-27" = "--user-data-dir=/tmp/playwright_chromiumdev_profile-N45EXg"
[pid=21705][err] "switch-26" = "--no-sandbox"
[pid=21705][err] "switch-25" = "--no-service-autorun"
[pid=21705][err] "switch-24" = "--use-mock-keychain"
[pid=21705][err] "switch-23" = "--password-store=basic"
[pid=21705][err] "switch-22" = "--enable-automation"
[pid=21705][err] "switch-21" = "--no-first-run"
[pid=21705][err] "switch-20" = "--metrics-recording-only"
[pid=21705][err] "switch-19" = "--force-color-profile=srgb"
[pid=21705][err] "switch-18" = "--disable-sync"
[pid=21705][err] "switch-17" = "--disable-renderer-backgrounding"
[pid=21705][err] "switch-16" = "--disable-prompt-on-repost"
[pid=21705][err] "switch-15" = "--disable-popup-blocking"
[pid=21705][err] "switch-14" = "--disable-ipc-flooding-protection"
[pid=21705][err] "switch-13" = "--disable-hang-monitor"
[pid=21705][err] "switch-12" = "--allow-pre-commit-input"
[pid=21705][err] "switch-11" = "--disable-features=ImprovedCookieControls,LazyFrameLoading,Globa"
[pid=21705][err] "switch-10" = "--disable-extensions"
[pid=21705][err] "switch-9" = "--disable-dev-shm-usage"
[pid=21705][err] "switch-8" = "--disable-default-apps"
[pid=21705][err] "switch-7" = "--disable-component-extensions-with-background-pages"
[pid=21705][err] "switch-6" = "--disable-client-side-phishing-detection"
[pid=21705][err] "switch-5" = "--disable-breakpad"
[pid=21705][err] "switch-4" = "--disable-backgrounding-occluded-windows"
[pid=21705][err] "switch-3" = "--disable-background-timer-throttling"
[pid=21705][err] "switch-2" = "--enable-features=NetworkService,NetworkServiceInProcess"
[pid=21705][err] "switch-1" = "--disable-background-networking"
[pid=21705][err] "num-switches" = "30"
[pid=21705][err] "osarch" = "x86_64"
[pid=21705][err] "pid" = "21705"
[pid=21705][err] "ptype" = "browser"
[pid=21705][err]
[pid=21705][err] [1006/002916.644528:ERROR:process_memory_range.cc(86)] read out of range
[pid=21705][err] [1006/002916.644582:ERROR:elf_image_reader.cc(606)] missing nul-terminator
[pid=21705][err] [1006/002916.646240:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[pid=21705][err] [1006/002916.646256:ERROR:file_io_posix.cc(144)] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
[pid=21705][err] Received signal 6
[pid=21705][err] #0 0x55e847d04f99 base::debug::CollectStackTrace()
[pid=21705][err] #1 0x55e847c6f513 base::debug::StackTrace::StackTrace()
[pid=21705][err] #2 0x55e847d04a71 base::debug::(anonymous namespace)::StackDumpSignalHandler()
[pid=21705][err] #3 0x7fb94271f140 (/usr/lib64/libpthread-2.18.so+0x1013f)
[pid=21705][err] #4 0x7fb93dfdb2e7 __GI_raise
[pid=21705][err] #5 0x7fb93dfdc6c8 __GI_abort
[pid=21705][err] #6 0x55e847d03d05 base::debug::BreakDebuggerAsyncSafe()
[pid=21705][err] #7 0x55e847c8333f logging::LogMessage::~LogMessage()
[pid=21705][err] #8 0x55e847c83a5e logging::LogMessage::~LogMessage()
[pid=21705][err] #9 0x55e844f0aa29 ui::(anonymous namespace)::OzonePlatformX11::InitializeUI()
[pid=21705][err] #10 0x55e844ea8067 ui::OzonePlatform::InitializeForUI()
[pid=21705][err] #11 0x55e8493da507 aura::Env::Init()
[pid=21705][err] #12 0x55e8493da49a aura::Env::CreateInstance()
[pid=21705][err] #13 0x55e8458a409f content::BrowserMainLoop::InitializeToolkit()
[pid=21705][err] #14 0x55e8458a4a4d content::BrowserMainRunnerImpl::Initialize()
[pid=21705][err] #15 0x55e8458a0dfb content::BrowserMain()
[pid=21705][err] #16 0x55e84780c692 content::ContentMainRunnerImpl::RunBrowser()
[pid=21705][err] #17 0x55e84780c157 content::ContentMainRunnerImpl::Run()
[pid=21705][err] #18 0x55e8478099c5 content::RunContentProcess()
[pid=21705][err] #19 0x55e84780a49e content::ContentMain()
[pid=21705][err] #20 0x55e8445ca226 ChromeMain
[pid=21705][err] #21 0x7fb93dfc7ac5 __libc_start_main
[pid=21705][err] #22 0x55e8445ca02a _start
[pid=21705][err] r8: 0000000000000000 r9: 0000000000000a00 r10: 0000000000000008 r11: 0000000000000206
[pid=21705][err] r12: 00001d8600105400 r13: 00007ffd02024720 r14: 00001d8600105410 r15: aaaaaaaaaaaaaaaa
[pid=21705][err] di: 00000000000054c9 si: 00000000000054c9 bp: 00007ffd02024710 bx: 00007ffd02024f50
[pid=21705][err] dx: 0000000000000006 ax: 0000000000000000 cx: ffffffffffffffff sp: 00007ffd020245d8
[pid=21705][err] ip: 00007fb93dfdb2e7 efl: 0000000000000206 cgf: 0000000000000033 erf: 0000000000000000
[pid=21705][err] trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
[pid=21705][err] [end of stack trace]
============================================================

解决方案:

错误信息显示是因为没有设置 DISPLAY 环境变量导致的。
Playwright 在使用 Chromium 时,需要正确设置DISPLAY环境变量,否则会报这种错误。

方式1:
直接在代码中设置 DISPLAY 环境变量:

import os
os.environ["DISPLAY"] = ":0"

# playwright code

方式2:
使用 headless 模式,不需要Xserver:

browser = await playwright.chromium.launch(headless=True)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值