简体   繁体   English

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

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

I am using the following code to run my script in local machine我正在使用以下代码在本地机器上运行我的脚本

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

I pushed this code onto git and then tried to run the same in jenkins using following build commands:我将此代码推送到 git 上,然后尝试使用以下构建命令在 jenkins 中运行相同的代码:

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

But then jenkins threw an error that chromedriver cannot be located.但是随后jenkins抛出了chromedriver无法定位的错误。 My questions are:我的问题是:

  1. Do I need to upload chromedriver.exe as well into my git repository我是否还需要将chromedriver.exe上传到我的 git 存储库中
  2. Does jenkins have its own chrome browser? jenkins有自己的chrome浏览器吗? If yes how do I use it and what path has to be specified?如果是,我该如何使用它以及必须指定什么路径?

I am new to jenkins, please help me out here我是 jenkins 的新手,请在这里帮帮我

  1. Check chrome version in Jenkins system Download chrome driver based on Jenkins system from here检查 Jenkins 系统中的 chrome 版本 从这里下载基于 Jenkins 系统的 chrome 驱动程序
  2. Copy the chrome driver to "C:/drivers/" in Jenkins server (As C driver is common to all windows system) update code as below将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)

as作为

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

Let me know if you face any issues with this.如果您遇到任何问题,请告诉我。

NOTE :注意

  1. In local system please move driver to "C:/driver" so that both remote and local system path is same.在本地系统中,请将驱动程序移动到“C:/driver”,以便远程和本地系统路径相同。
  2. If chrome version is updated in local or remote, please update chrome driver version ie chromedriver.exe如果本地或远程更新chrome版本,请更新chrome驱动版本,即chromedriver.exe

I found the solution.我找到了解决方案。 My code was missing the chrome binary path.我的代码缺少 chrome 二进制路径。 Adding the same as an Options() argument resolved the error.添加与Options()参数相同的参数解决了该错误。
Sharing the updated patch of code:分享更新的代码补丁:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM