Chrome 在WSL2上运行“ Puppeteer ”

cld4siwp  于 2022-12-06  发布在  Go
关注(0)|答案(1)|浏览(188)

有没有一种简单的方法可以在wsl2上运行Puppeteer?我已经尝试了很多解决方案,也看了很多论坛来寻找答案。到目前为止还没有成功,我发现在windows上运行Puppeteer非常容易,但在wsl2上运行却非常困难。
我试过下载chrome和chrome,并指定executablePath,但没有成功。
我知道它可以在我的Windows系统上工作,但似乎不能让Puppeteer在WSL2中运行。
我目前遇到此错误:

Timeout Error: Timed out after 30000 ms while trying to connect to the browser! Only Chrome at revision r1022525 is guaranteed to work.

我看node_modules的时候发现1022525版本正在使用中,我不知道人偶师的问题是什么,也不知道该怎么解决。
有人有什么想法吗?

czfnxgou

czfnxgou1#

As I do not see exactly what are you trying to do , in the moment I can think that this error accused because of your windows defender firewall, which do not allow access of your X-server.
anyway for me I use Puppeteer on wsl2 as following:

  1. I need to install VcXsrc as X-server to display what is going on in the Linux side of the computer. https://sourceforge.net/projects/vcxsrv notes: you need to allow “Public networks” when the Windows firewall pops up.
  2. You need now to set a DISPLAY environment variable has to be set,becuase wsl has its own IP address, we need to connect to our X-server app using this IP so to do this follow the next steps.
  3. In your .bashrc (or .zshrc if you are using ZSH) add this line exactly to the end: export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
  4. now executes the content of your bash use source ~/.bashrc (or source ~/.zshrc if you are using ZSH)
  5. to be sure that your DISPLAY environment variable has set correctly , try this command (echo $DISPLAY) the result should be something like this : # 172.16.210.1:0.0 it will be your wsl IP address.
  6. now Open the VcXsrv program in Windows (called XLaunch), allow this options step after step “Multiple windows” and “Start no client”. and in the end tick the option of disable access control Be sure to disable access control.
  7. as last check go to windows defender firewall and be sure that you enable all network connection for VcXSrv. now you are ready to go, let suppose I am using puppeteer to test inside jest file like the follwoing :
    const puppeteer = require('puppeteer'); test('should create an element...etc', async () => { const browser = await puppeteer.launch({ headless: false, args: ['--window-size=1920,1080'] }); const page = await browser.newPage(); await page.goto( 'www.google.com' ); });
    start the last code using (jest nameofyourtestfile) and your puppeteer will work as you like, Chromium will open in your windows ...enjoy...if there is any details which is not clear may you ask again. I hope that I helped

相关问题