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

↘锁芯ラ 提交于 2019-12-10 02:00:44

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/38173541/how-to-resolve-chromedriver-executable-needs-to-be-in-path-error-when-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!