1

I have a following problem.

In my scrape.py I set WebDriver using a function nastav_driver():

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.webdriver import WebDriver

import utils


def nastav_driver() -> WebDriver:
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(utils.PATH_CHROMIUM, options=options)
    return driver

where utils.PATH_CHROMIUM is /usr/lib/chromium-browser/chromedriver. When I run the script, everything is OK. But When I run a docker image using sudo docker run python:0.0.1 I got this error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/usr/local/lib/python3.10/subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.10/subprocess.py", line 1842, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/chromium-browser/chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/scrape_prehled.py", line 242, in <module>
    main()
  File "/app/scrape_prehled.py", line 201, in main
    driver = nastav_driver()
  File "/app/scrape_prehled.py", line 36, in nastav_driver
    driver = webdriver.Chrome(utils.PATH_CHROMIUM, options=options)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

What is the problem here, please?

vojtam
  • 1,157
  • 9
  • 34

1 Answers1

1

You should define and put the web driver exe file somewhere inside your selenium project so as when you run your project in a docker or on some other machine, for example on Windows PC, it will still work correctly.
For example my FireFox driver is located here:

./src/main/resources/geckodriver.exe

So the code line is

System.setProperty("webdriver.gecko.driver", "./src/main/resources/geckodriver.exe");

The same for Chrome and Edge drivers.
You can set the specific location of the driver exe files in your project accordingly to your project actual structure.
The syntax above is in Java, but with Python it will be quite similar.

Prophet
  • 32,350
  • 22
  • 54
  • 79