我正在创建一个跨平台的Python脚本,使用Selenium执行一些命令。
我有两个问题:
- 为什么以下脚本在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
- 我该如何创建一个使用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。