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

查看:80
本文介绍了如何在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上,我使用的是Chrome84.我从 ChromeDriver-适用于Chrome的WebDriver .并将.exe安装在C:\ ChromeDriver \ chromedriver.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:\ChromeDriver\chromedriver.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和硒从WSL2启动Chrome驱动程序?

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

推荐答案

适用于尚未找到解决方案的人员.请遵循本教程: WSL2中的chromedriver 许多是相似的,但是对我来说,诀窍是将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天全站免登陆