Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "chromedriver executable needs to be in PATH" error when running Selenium Chrome using virtualenv within PyDev?

Short:

Having read Controlling the Browser with the selenium Module at https://automatetheboringstuff.com/chapter11, I am trying to run the Selenium Chrome driver in a virtual environment from PyDev. I have managed to do it from outside PyDev, but from within, I get:

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

Long:

I'm using Linux Debian 3.10.11-1.

Following https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/, before even starting with PyDev, I . . .

  1. Set up a virtual environment with virtualenv

    1. Installed virtualenv

      pip install virtualenv
    2. Made a directory for my project

      cd ~/temp/
      mkdir my_project
      cd my_mproject
    3. Created a virtual environment called env

      virtualenv env
    4. Activated it.

      source env/bin/activate
    5. Went into Python's interactive mode to tell myself which version of Python I was using

      python
      Python 2.7.12rc1 (default, Jun 13 2016, 09:20:59) 
      [GCC 5.4.0 20160609] on linux2
  2. Exited out of the interacive mode and installed the Selenium stuff

    1. First the module

      pip install selenium
    2. Following suggestion at https://groups.google.com/forum/#!topic/obey-the-testing-goat-book/Ty4FQoV3S0Q, installed chromedriver

      1. cd env/bin/
      2. wget http://chromedriver.storage.googleapis.com/2.22/chromedriver_linux64.zip
      3. unzip chromedriver_linux64.zip
  3. Wrote a little script to see if it would work

    from selenium import webdriver
    driver = webdriver.Chrome()

    Ran it. A Chrome web-browser window opened. Great.

Now to try it in PyDev:

Roughly following https://www.caktusgroup.com/blog/2011/08/31/getting-started-using-python-eclipse/ . . .

  1. Installed Eclipse (Neon)

  2. Installed PyDev

  3. From within Eclipse, created a new project (File menu > New Project > General > Project, entered a Project Name and clicked Finish).

  4. Back at the command prompt (because I haven't yet found out how to do this from within Eclipse and PyDev): cd'ed into my new project's root directory and created a virtual environment.

  5. As before, still at the command prompt, I activated the new project's virtual environment and installed the Selenium module and then the chromedriver executable file that came up of the chromedriver_linux64.zip file.

  6. Back in Eclipse, I signed up my project to use the virtual environment, which I guess in PyDev lingo is called not a virtual environment, but rather an interpreter:

    1. Window menu > Preferences > PyDev > Interpreters > Python Interpreters > Add.

    2. Gave the interpreter a name.

    3. For Interpreter Executable, I selected the python2.7 file in my project's virtual environment's bin directory

    4. Right-clicked on my project, select Properties > PyDev - Interpreter/Grammar > Under Interpreter selected my new interpreter > OK.

  7. Gave my project the same script . . .

    from selenium import webdriver
    driver = webdriver.Chrome()

    and ran it by clicking on the Run menu > Run As > Python Run.

    Now, though, instead of a Chrome web-browser window opening, I get only a message in Eclipse's console:

    selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

To get the web browser window to open as it does when I run the same scrip from a command prompt, I have tried:

  • adding the the virtual environment's bin folder (because that's where the chromedriver file is) to the interpreter.

  • deleting the interpreter and creating it new.

  • copying the chromedriver into the same directory where my script is. No difference.

  • adding the chromedriver_linux64.zip file that I downloaded to the interpreter. Still the same error.

I been continuing by writing my script in PyDev, then turning to the command prompt to run it. Just wish I could run it in PyDev's debug mode.

How can I get this 'chromedriver' in the "PATH" in PyDev so I can run the script from Eclipse?

like image 467
user1930469 Avatar asked Jul 03 '16 19:07

user1930469


People also ask

How do I fix Chromedriver executable in path?

To solve the Selenium error "WebDriverException: Message: 'chromedriver' executable needs to be in PATH", install and import the webdriver-manager module by running pip install webdriver-manager . The module simplifies management of binary drivers for different browsers.

How do I add Chromedriver executable to path?

Go to the terminal and type the command: sudo nano /etc/paths. Enter the password. At the bottom of the file, add the path of your ChromeDriver. Type Y to save.

How do we set the path of driver executable?

getting java lang IllegalStateException The path to the driver executable must be set by the webdriver chrome driver system property. WebDriver driver = new ChromeDriver(); System. setProperty("webdriver.


2 Answers

Not sure if this is the best thing to do, but I have found something that seems to work: I have added to my interpreter the already available variable named PATH, and I have edited that variable's value to include the relative path to my project's virtual environment's bin directory (ie, the directory where I have the chromedriver executable file saved).

More precisely:

  1. Window menu in Eclipse > Preferences > PyDev on the left > Interpreters > Python Interpreters.

  2. Selected the interpreter that I had created earlier for my project (as descibed in the question above)

  3. Switched from Libaries to Environment in the bottom half of the Preferences window

  4. Clicked on the Select... button on the right.

    A list of Environment Variable appeared.

  5. Scrolled down through the list and found one named PATH. Selected it and click on the OK button.

    It and its value (/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games) appeared in the otherwise empty list.

  6. I selected it and clicked on Edit...

    Its name and value became editable.

  7. To the right-hand end of the value I added :env/bin (ie, the relative path from the directory holding my script to my project's virtual environment's bin directory).

  8. Clicked OK to get back to the Preferences window > Apply in the Preferences window > OK to close the Preferences window.

  9. Ran the program from within Eclipse (selected the script file > Run menu > Run As > Python Run).

A Chrome (well, Chromium - this is Debian) window opened just as had been happening when I was running my program from the command prompt.

Great.

like image 76
user1930469 Avatar answered Sep 27 '22 21:09

user1930469


If all attempts to put chromedriver in your PATH fail, you can also hand the executable path to webdriver.Chrome() like so:

chromedriver_loc = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=chromedriver_loc)

This was my eventual solution when trying to run chromedriver from a virtualenv.

like image 45
tterry Avatar answered Sep 27 '22 21:09

tterry