Jenkins 抛出错误 Errno 2] 运行 selenium 作业时没有这样的文件或目录用于 chrome 驱动程序路径

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

在 jenkins 上运行一个简单的 selenium 作业(jenkins 在 azure 服务器实例上的 docker 容器中运行)抛出错误:

[Errno 2] No such file or directory: '/root/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver'

File "/var/jenkins_home/workspace/Historical Volume 
Job/cronjobs/extractCookies.py", line 34, in extract_cookie
    driver = webdriver.Chrome(chrome_options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/usr/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 53, in __init__
    self.service.start()
  File "/usr/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 105, in start
    self._start_process(self._path)
  File "/usr/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 207, in _start_process
    self.process = subprocess.Popen(
                   ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.11/subprocess.py", line 1950, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/root/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver'

我已检查该文件确实存在于该位置。这仅发生在詹金斯上,但在我的本地系统上运行良好。这就是 jenkins 传递给 python 的子进程模块 subprocess.Popen cmd ['/root/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver', '--port=47841']

selenium-webdriver jenkins selenium-chromedriver subprocess
1个回答
0
投票

您应该使用

playwright
而不是
selenium
Playwright
是用于 Web 浏览器自动化的较新的 Python 库之一。

像这样安装:

pip install playwright
,并安装浏览器:

playwright install
,一旦运行此命令,就会下载 chromium、webkit 和 firefox。别担心,您仍然可以使用 chrome 和 msedge!只需要在代码中添加一个参数即可:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(channel="chrome") #allows usage of chrome
    page = browser.new_page()
    page.goto('https://example.com')
    page.locator('button#example').click() #clicks button called 'example'
    browser.close()

这将起作用,无需网络驱动程序。请注意,

playwright
默认情况下以无头模式运行,因此您需要在启动浏览器时将参数切换为 false 才能看到浏览器打开。

此外,如果您使用 chrome 或 msedge,请使用以下行:

browser = p.chromium.launch(channel="chrome or msedge")
,但如果您使用其他浏览器,请取消通道参数,只需说:
browser = p.firefox.launch()
,我以 Firefox 为例.

Playwright
是一个更好的用于网络浏览器自动化的Python库,不需要网络驱动程序,并且作为较新的Python库之一,它当然有其优势,特别是因为它可以与Python以外的其他流行编程语言一起使用。

Playwright 文档:https://playwright.dev/python/docs/intro

© www.soinside.com 2019 - 2024. All rights reserved.