如何在 wsl2 上从 python3 运行 Selenium ChromeDriver? [英] How to run Selenium ChromeDriver from python3 on wsl2?

查看:26
本文介绍了如何在 wsl2 上从 python3 运行 Selenium ChromeDriver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 3 从 WSL2(Ubuntu 18.04)无头打开 Chrome.

I'm trying to headless open Chrome from WSL2 (Ubuntu 18.04) using python 3.

在 Windows 上,我使用的是 Chrome 84.我从 ChromeDriver - Chrome 的 WebDriver.并在 C:ChromeDriverchromedriver.exe 下安装了.exe

On Windows I'm using Chrome 84. I've downloaed Chrome Driver 84 from ChromeDriver - WebDriver for Chrome. And installed the .exe under C:ChromeDriverchromedriver.exe

我已经设置了一个从我的 Windows Chrome 和 ChromeDriver 到 WSL2 的符号链接:

I've set a symbolic link from my Windows Chrome and ChromeDriver to WSL2:

sudo ln -s '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' /usr/bin/google-chrome
sudo ln -s /mnt/c/ChromeDriver/chromedriver.exe /usr/bin/chromedriver

两个 Chrome 都设置为可由 WSL2 上的任何用户执行.

Both Chromes are set to be executable by any user on WSL2.

在 WSL2 上,当我进入控制台时:

On WSL2, when I enter in the console:

google-chrome --use-gl=swiftshader

Chrome 在 Windows 上启动.

Chrome starts on windows.

这是我的脚本:

from selenium import webdriver
browser = webdriver.Chrome()    # fails
# browser = webdriver.Chrome('/usr/bin/chromedriver') fails
# browser = webdriver.Chrome('/mnt/c/ChromeDriver/chromedriver.exe') fails
browser.get('https://stackoverflow.com')

失败并出现错误:

raise WebDriverException("无法连接到服务 %s" %self.path)selenium.common.exceptions.WebDriverException:消息:可以未连接到服务 chromedriver(* 或/usr/bin/chromedriver 或/mnt/c/ChromeDriver/chromedriver.exe,具体取决于我如何启动 webdriver.Chrome())

raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver (* OR /usr/bin/chromedriver OR /mnt/c/ChromeDriver/chromedriver.exe depending on how I start webdriver.Chrome())

如何使用 python3 和 selenium 从 WSL2 启动 Chrome 驱动程序?

How to be able to start Chrome Driver from WSL2 using python3 and selenium?

推荐答案

对于那些还没有找到解决方案的人.遵循本教程:chromedriver in WSL2许多都是相似的,但对我来说,诀窍是将 chromedriver 放在相应的组和用户中:

For those who have not yet found the solution. Follow this tutorial: chromedriver in WSL2 Many are similar, but what did the trick for me was to place the chromedriver in the corresponding group and user:

sudo chown root:root /usr/bin/chromedriver

这篇关于如何在 wsl2 上从 python3 运行 Selenium ChromeDriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆