Linux32平台下的Chromedriver不存在 - Python Selenium Chromedriver

4

我正在创建一个跨平台的Python脚本,使用Selenium执行一些命令。

我有两个问题:

  1. 为什么以下脚本在Windows上可以工作,但在Raspberry Pi OS 32位上无法工作?唯一的解决方法是删除webdriver-manager,但这需要手动安装webdriver。我正在使用树莓派3。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType

options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=options)
driver.get("http://www.google.com")
print(driver.title)

输出结果如下:
pi@raspberrypi:~/Documents/Software $ /bin/python /home/pi/Documents/Software/test.py


====== WebDriver manager ======
Current chromium version is 95.0.4638
Get LATEST chromedriver version for 95.0.4638 chromium
There is no [linux32] chromedriver for browser  in cache
Trying to download new driver from https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip
Traceback (most recent call last):
  File "/home/pi/Documents/Software/test.py", line 10, in <module>
    driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=options)
  File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/chrome.py", line 32, in install
    driver_path = self._get_driver_path(self.driver)
  File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/manager.py", line 30, in _get_driver_path
    file = download_file(driver.get_url(), driver.ssl_verify)
  File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/utils.py", line 98, in download_file
    validate_response(response)
  File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/utils.py", line 80, in validate_response
    raise ValueError("There is no such driver by url {}".format(resp.url))
ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip
  1. 我该如何创建一个使用Selenium WebDriver在无头模式下工作并在每个平台上都能运行的Python脚本?我的意思是,如果我在脚本中使用chromewebdriver,那么使用脚本的用户必须安装Chrome,同样地,如果使用Firefox,则用户必须安装Firefox。是否有一种WebDriver可以在不需要外部脚本安装的情况下工作?

谢谢

编辑:

问题不在于WebDriver Manager,而在于针对Linux32位系统不存在Chromium的ChromeDriver。事实上,在地址"https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip"中没有ChromeDriver,但将linux32替换为linux64后会下载一个软件包,但与linux32不兼容。 我不明白的是,如果针对Linux32位系统不存在ChromeDriver,那么为什么通过“sudo apt-get install chromium-chromedriver”安装它们,然后从代码中删除WebDriver-Manager调用,Python脚本就可以工作呢?其实是存在Linux32位系统的ChromeDriver,只是它们不在主要的ChromeDriver网站上。 我正在使用带有Chromium 95.0.4638.69的Raspberry Pi 3。


尝试在树莓派上更新PIP。 - pcalkins
你可以将代码转换为Java并使用HTMLUnit。Java有自己的运行时,而HTMLUnit可以轻松地与您的.jar文件打包。否则,您将不得不安装特定的浏览器,而您的安装文件将非常大...(我制作了一个包含Windows XP和向前兼容版本的Chrome、Webdrivers、Selenium和程序的软件包,大小约为96MB。) - pcalkins
@pcalkins 嘿,谢谢你的建议,我尝试更新pip,但它已经是最新版本了,脚本仍然无法运行。问题似乎出在webdriver-manager没有提供webdriver。 不过,我通过删除第10行的log_level = 0来更新了代码,这样输出错误信息更详细了。 - ernestocesario
当您使用apt-get时,它会安装哪个版本的chromedriver?在Windows中,您只需双击它,它就会在控制台中显示版本。或者使用chromedriver -v启动。 - pcalkins
webdriver-manager在Windows和Linux64上运行得非常完美,因为它可以找到要下载的文件。但是,在Rpi 3上,它是基于Linux32的树莓派操作系统,没有Chrome浏览器,也没有适用于Linux32的ChromeDriver,但是有Chromium浏览器,并且可以使用apt-get从树莓档案中下载适用于Chromium的ChromeDriver。 'http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-chromedriver_95.0.4638.78-rpt6_armhf.deb'树莓派上的Chromium版本为:95.0.4638.78 - ernestocesario
看起来那些驱动程序是由Raspian项目团队编译的。因此,webdrivermanager必须指向该操作系统的正确存储库。(Google不再制作32位驱动程序...) - pcalkins
1个回答

10
  1. 由于官方团队尚未开发linux32的chromedriver(例如https://chromedriver.storage.googleapis.com/index.html?path=99.0.4844.17/),因此无法使用webdriver_manager,因为它正在查看官方仓库。

但是,Raspian团队维护了兼容版本的chromedriver,因此为了使其正常工作,您需要:

  • Install chromedriver for raspberry. e.g. for Linux:

    sudo apt-get install chromium-chromedriver
    
  • When you instantiate your driver, you need to tell him where to find this chromedriver. Plus, you should also state that you use Chromium instead of Chrome (if so):

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    options = Options()
    # browser is Chromium instead of Chrome
    options.BinaryLocation = "/usr/bin/chromium-browser"
    # we use custom chromedriver for raspberry
    driver_path = "/usr/bin/chromedriver"
    driver = webdriver.Chrome(options=options, service=Service(driver_path))
    
  • Then you're good to go:

    driver.get("https://stackoverflow.com/")
    
  1. If you need a code that works for every platform, the best option I found so far is to distinguished raspberry pi from the other system:

    import platform       
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    options = Options()
    # a few usefull options
    options.add_argument("--disable-infobars")
    options.add_argument("start-maximized")
    options.add_argument("--disable-extensions")
    options.add_argument("--headless") # if you want it headless
    
    if platform.system() == "Linux" and platform.machine() == "armv7l":  
        # if raspi
        options.BinaryLocation = ("/usr/bin/chromium-browser")
        service = Service("/usr/bin/chromedriver")
    else: # if not raspi and considering you're using Chrome
        service = Service(ChromeDriverManager().install())           
    
    driver = webdriver.Chrome(
        service=service,
        options=options
        )
    

针对您关于不需要手动安装Chrome以使Selenium工作的问题,您可以通过脚本安装Chrome,以确保用户已经安装了它。这也适用于Docker使用,例如在带有Linux amd64环境的DockerFile中:

   RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
   RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy instal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接