简体   繁体   中英

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:

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. My questions are:

  1. Do I need to upload chromedriver.exe as well into my git repository
  2. Does jenkins have its own chrome browser? If yes how do I use it and what path has to be specified?

I am new to jenkins, please help me out here

  1. Check chrome version in Jenkins system Download chrome driver based on Jenkins system from here
  2. Copy the chrome driver to "C:/drivers/" in Jenkins server (As C driver is common to all windows system) update code as below
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.
  2. If chrome version is updated in local or remote, please update chrome driver version ie chromedriver.exe

I found the solution. My code was missing the chrome binary path. Adding the same as an Options() argument resolved the error.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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