在 linux-arm64 上使用 Selenium 是否有已知的工作配置?

问题描述 投票:0回答:1

我编写了一个快速程序,可以按照设定的时间间隔使用 Firefox 打开和关闭网站。它在我的 Intel Mac OS Ventura 上完美运行。我打算让它在 Raspberry Pi 上运行,但我找不到可以在其上运行的 Selenium、geckodriver 或 chromedriver 以及 Firefox 或 Chromium 版本的组合。

Mac 可以运行 Selenium 4.11.2、geckodriver v0.33.0 和 Firefox 115.0.3。 Raspberry Pi 具有 Ubuntu 22.04.3 LTS。我在这里发现,https://github.com/SeleniumHQ/selenium/issues/11599,Selenium Manager 不适用于 linux-arm64,而 Raspberry Pi 使用 linux-arm64。即使当我尝试在驱动程序的路径中编码时,我也会遇到错误,Selenium 记录它找不到驱动程序,即使它也在 PATH 中。看起来开发人员在上面的对话中说内置的 Selenium Manager 驱动程序管理器会导致此类错误。 Selenium Manager 是在 Selenium 4.6 中引入的,因此我回滚到 Selenium 4.5,更改了该版本的代码,尝试运行它,并得到了不同的错误,这些错误似乎与驱动程序和 Firefox 版本之间的不兼容问题有关。我尝试了它们的不同组合,但没有成功。然后我决定尝试使用 Chrome。 Google 没有为 linux-arm64 提供 chromedriver 版本,因此我尝试使用此处找到的不同版本,https://github.com/electron/ Electron/releases,以及尝试回滚 Chromium。我至少能够使用该程序启动 Chromium 浏览器,这比使用 Firefox 更成功,但我无法让它完全运行。在整个过程中,我在 Stack Overflow 上阅读了许多 Selenium 问题的答案,但没有任何帮助。

以下是使用上述配置在 Mac 上运行良好的代码:

import datetime, logging, time
from selenium import webdriver

logger = logging.getLogger('selenium')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler("handler.log")
logger.addHandler(handler)
logging.getLogger('selenium.webdriver.remote').setLevel(logging.DEBUG)
logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)
logging.basicConfig(filename="program.log", level=logging.INFO)
timeStarted = datetime.datetime.now()
logging.info(
    timeStarted.strftime("%m/%d/%Y, %H:%M:%S")
    + "   started on https://google.com"
)
# Program starts here
while True:
    timeOfRequest = datetime.datetime.now()
    try:
        browser = webdriver.Firefox()
        browser.get("https://google.com")
        logging.info(
            timeOfRequest.strftime("%m/%d/%Y, %H:%M:%S")
            + "   Success"
        )
    except:
        logging.exception(
            timeOfRequest.strftime("%m/%d/%Y, %H:%M:%S") + "   Something went wrong"
        )
    time.sleep(810)
    browser.quit()
    time.sleep(30)
python selenium-webdriver selenium-chromedriver arm64 geckodriver
1个回答
0
投票

这是我花了一整晚尝试解决这个问题后得到的结果:

要在 linux-arm64 上使用 Selenium,我们需要获取三个旧的 Debian 软件包:

以下是分步指南。

安装

下载软件包

要直接从启动板获取必要的 Debian 软件包,我们可以使用

wget
命令。

# Fetch chromium-codecs-ffmpeg-extra
wget http://launchpadlibrarian.net/660838579/chromium-codecs-ffmpeg-extra_112.0.5615.49-0ubuntu0.18.04.1_arm64.deb

# Fetch chromium-browser
wget http://launchpadlibrarian.net/660838574/chromium-browser_112.0.5615.49-0ubuntu0.18.04.1_arm64.deb

# Fetch chromium-chromedriver
wget http://launchpadlibrarian.net/660838578/chromium-chromedriver_112.0.5615.49-0ubuntu0.18.04.1_arm64.deb

准备好后,可以使用

gdebi-core
安装它们,这将处理依赖关系。

安装gdebi-core

sudo apt-get install gdebi-core

安装 Debian 软件包

sudo gdebi chromium-codecs-ffmpeg-extra_112.0.5615.49-0ubuntu0.18.04.1_arm64.deb
sudo gdebi chromium-browser_112.0.5615.49-0ubuntu0.18.04.1_arm64.deb
sudo gdebi chromium-chromedriver_112.0.5615.49-0ubuntu0.18.04.1_arm64.deb

按照这些步骤操作后,应该可以在 linux-arm64 上使用 ChromeDriver 进行 Selenium 的工作配置。

验证安装

chromium-browser --version

输出应该像

Chromium 112.0.5615.49 Built on Ubuntu , running on Ubuntu 22.04

要进一步测试安装,请将以下脚本保存为 test.py。请注意,为选项提供的参数至关重要:

from selenium import webdriver

# Initialize the Chrome WebDriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
# options.add_argument('--disable-dev-shm-usage')
# options.add_argument('--remote-debugging-port=9222') 


driver = webdriver.Chrome(options=options)

# Retrieve the capabilities
capabilities = driver.capabilities

# For Chrome:
if 'browserName' in capabilities and capabilities['browserName'] == 'chrome':
    browser_version = capabilities.get('browserVersion', 'Unknown')
    chromedriver_version = capabilities.get('chrome', {}).get('chromedriverVersion', 'Unknown').split(' ')[0]
    print(f"Browser Name: Chrome")
    print(f"Browser Version: {browser_version}")
    print(f"ChromeDriver Version: {chromedriver_version}")

# Close the driver
driver.quit()

执行

python test.py
应该产生:

Browser Name: Chrome
Browser Version: 112.0.5615.49
ChromeDriver Version: 112.0.5615.49
© www.soinside.com 2019 - 2024. All rights reserved.