【问题标题】:How to setup chromedriver in jenkins for selenium-python script?如何在 jenkins 中为 selenium-python 脚本设置 chromedriver?
【发布时间】:2021-06-01 17:17:26
【问题描述】:

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

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 抛出了无法找到 chromedriver 的错误。我的问题是:

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

我是詹金斯的新手,请在这里帮助我

【问题讨论】:

  • 请说明 Jenkins 设置是在 Windows 还是 Linux 中。需要注意的事项清单。 - 操作系统 - 系统中的浏览器版本(获取适当的驱动程序) - 驱动程序路径和执行权限
  • @vjk:它在 Windows 中的设置。你能帮我列一下清单吗?

标签: python selenium jenkins jenkins-pipeline


【解决方案1】:
  1. 在 Jenkins 系统中检查 chrome 版本 从here下载基于Jenkins系统的chrome驱动
  2. 将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

【讨论】:

  • 如何在 Jenkins 系统中查看 chrome 版本?
  • 取远程windows系统的RDP,可以手动检查,也可以尝试从jenkins自己执行bat cmd "reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version"
  • @LibinThomas 你能解决这个问题吗?
  • 非常感谢您的支持。我找到了解决方案。将其添加为评论
【解决方案2】:

我找到了解决方案。我的代码缺少 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

【讨论】:

    猜你喜欢
    • 2023-01-28
    • 2018-04-11
    • 2019-04-18
    • 2012-03-22
    • 1970-01-01
    • 2013-02-16
    • 2017-11-25
    • 2020-02-19
    • 1970-01-01
    相关资源
    最近更新 更多