如何在 jenkins 中为 selenium-python 脚本设置 chromedriver?

How to setup chromedriver in jenkins for selenium-python script?

我正在使用以下代码运行我在本地机器上的脚本

from seleniumwire import webdriver
import pytest
from selenium.webdriver.chrome.options import Options
import time
import allure

class Test_main():

    @pytest.fixture()
    def test_setup(self):
        # instantiate browser
        chrome_options = Options()
        chrome_options.add_argument('--start-maximized')
        chrome_options.add_argument('--headless')
        self.driver = webdriver.Chrome(executable_path=r"D:/Python/Sel_python/drivers/chromedriverv86/chromedriver.exe", chrome_options=chrome_options)

        # terminate script
        yield
        self.driver.close()
        self.driver.quit()
        print("Test completed")

##Remaining functions/test cases followed. Not adding the entire script here

我将此代码推送到 git,然后尝试使用以下构建命令在 jenkins 中 运行 执行相同的操作:

cd "D:\Python\Sel_python\Pytest"
pip install -r requirements.txt
pytest Test_Tracking_code_scripts.py -s -v

但是随后 jenkins 抛出了一个错误,即 chrome无法定位驱动程序。我的问题是:

  1. 我是否还需要将 chromedriver.exe 上传到我的 git 存储库
  2. jenkins 有自己的 chrome 浏览器吗?如果是,我该如何使用它以及必须指定什么路径?

我是 jenkins 的新手,请帮帮我

  1. 检查Jenkins系统中的chrome版本 从here
  2. 下载chrome基于Jenkins系统的驱动
  3. 将 chrome 驱动程序复制到 Jenkins 服务器中的“C:/drivers/”(因为 C 驱动程序对所有 windows 系统都是通用的) 更新代码如下
self.driver = webdriver.Chrome(executable_path=r"D:/Python/Sel_python/drivers/chromedriverv86/chromedriver.exe", chrome_options=chrome_options)

作为

self.driver = webdriver.Chrome(executable_path=r"C:/drivers/chromedriver.exe", chrome_options=chrome_options)

如果您遇到任何问题,请告诉我。

注意:

  1. 在本地系统中,请将驱动程序移动到“C:/driver”,以便远程和本地系统路径相同。
  2. 如果 chrome 版本在本地或远程更新,请更新 chrome 驱动程序版本即 chromedriver.exe

我找到了解决方案。我的代码缺少 chrome 二进制路径。添加与 Options() 相同的参数解决了错误。
分享更新的代码补丁:

from seleniumwire import webdriver
import pytest
from selenium.webdriver.chrome.options import Options
import time
import allure

class Test_main():

    @pytest.fixture()
    def test_setup(self):
        # initiating browser
        chrome_options = Options()
        chrome_options.binary_location=r"C:\Users\libin.thomas\AppData\Local\Google\Chrome\Application\chrome.exe"
        chrome_options.add_argument('--start-maximized')
        chrome_options.add_argument('--headless')

        self.driver = webdriver.Chrome(executable_path=r"D:/Python/Sel_python/drivers/chromedriver v86/chromedriver.exe",options=chrome_options)
       
        # terminate script
        yield
        self.driver.close()
        self.driver.quit()
        print("Test completed")

#test cases followed below