在PyDev中使用virtualenv运行Selenium Chrome时,如何解决“chromedriver可执行文件的路径”错误?

时间:2023-01-22 18:20:43

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:

在https://automatetheboringstuff.com/chapter11上阅读了使用selenium模块控制浏览器之后,我试图在PyDev的虚拟环境中运行selenium Chrome驱动程序。我已经设法从PyDev外面做了,但是从内部,我得到:

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

“chromedriver”可执行文件必须在路径中。

Long:

长:

I'm using Linux Debian 3.10.11-1.

我使用的是Linux Debian 3.10.11-1。

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

以下是https://www.dabapps.com/blog/tiontopip -and-virtualenv-python/,甚至在PyDev开始之前,我…

  1. Set up a virtual environment with virtualenv

    使用virtualenv设置虚拟环境

    1. Installed virtualenv

      安装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

      创建了一个名为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。

      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

    退出了interacive模式并安装了Selenium。

    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

      以下建议在https://groups.google.com/forum/ !主题/ obey-the-testing-goat-book / Ty4FQoV3S0Q chromedriver安装

      1. cd env/bin/
      2. cd env / bin /
      3. wget http://chromedriver.storage.googleapis.com/2.22/chromedriver_linux64.zip
      4. wget http://chromedriver.storage.googleapis.com/2.22/chromedriver_linux64.zip
      5. unzip chromedriver_linux64.zip
      6. 解压缩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.

    运行它。一个Chrome浏览器窗口打开了。太好了。

Now to try it in PyDev:

现在在PyDev网站上试试:

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

大致遵循https://www.caktusgroup.com/blog/2011/08/31/getting-started-using- pythoneclipse/…

  1. Installed Eclipse (Neon)

    安装Eclipse(霓虹灯)

  2. Installed PyDev

    PyDev中安装

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

    在Eclipse中,创建一个新项目(文件菜单>新项目>常规>项目,输入项目名称并单击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.

    回到命令提示符(因为我还没有发现如何在Eclipse和PyDev中执行此操作):cd进入我的新项目的根目录并创建一个虚拟环境。

  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.

    和以前一样,仍然在命令提示符下,我激活了新项目的虚拟环境,并安装了Selenium模块,然后是chromedriver_linux64生成的可执行文件。zip文件。

  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:

    在Eclipse中,我注册了我的项目,以使用虚拟环境,我猜在PyDev lingo中,这不是一个虚拟环境,而是一个解释器:

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

      窗口菜单>首选项> PyDev >解释器> Python解释器> 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

      对于解释器可执行文件,我在项目的虚拟环境的bin目录中选择了python2.7文件

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

      右键点击我的项目,选择属性> PyDev -解释器/语法>解释器在解释器下选择我的新解释器>。

  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.

    并通过单击Run菜单>运行> Python。

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

    但是现在,我在Eclipse的控制台中只看到一条消息,而不是Chrome浏览器窗口的打开:

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

    “chromedriver”可执行文件必须在路径中。

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

为了打开web浏览器窗口,就像我在命令提示符中运行相同的scrip一样,我尝试过:

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

    向解释器添加虚拟环境的bin文件夹(因为这是chromedriver文件所在的位置)。

  • deleting the interpreter and creating it new.

    删除解释器并创建新的解释器。

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

    将chromedriver复制到我的脚本所在的目录中。没有区别。

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

    添加chromedriver_linux64。zip文件,我下载到解释器。还是同样的错误。

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.

我继续在PyDev中编写脚本,然后转向命令提示符来运行它。但愿我能在PyDev的调试模式下运行它。

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

如何在PyDev的“路径”中获得这个“chromedriver”,这样我就可以从Eclipse运行脚本了?

2 个解决方案

#1


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).

不确定这是最好的办法,但我发现事情似乎工作:我已经添加到我的翻译已经可用的变量命名的道路,我编辑变量的值,包括我的项目的相对路径的虚拟环境的bin目录(即我chromedriver可执行文件的目录保存)。

More precisely:

更准确地说应该是:

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

    窗口菜单在Eclipse >首选项> PyDev的左侧>解释器> Python解释器。

  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

    从Libaries切换到Preferences窗口的下半部分的环境

  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.

    翻遍列表,发现了一条名为PATH的路径。选择它并单击OK按钮。

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

    它和它的价值(/usr/local/bin:/usr/bin:/ usr/local/games:/usr/games:/usr/local/ bin:/usr/bin:/bin:/ bin:/usr:/ local/games)出现在其他的空列表中。

  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).

    在值的右边,我添加了:env/bin(即从保存我的脚本的目录到项目虚拟环境的bin目录的相对路径)。

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

    单击OK回到Preferences窗口>应用于Preferences窗口> OK关闭Preferences窗口。

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

    在Eclipse中运行程序(选择脚本文件> Run菜单>作为> Python运行)。

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

一个Chrome(哦,铬——这是Debian)窗口打开了,就像我在命令提示符上运行程序时一样。

Great.

太好了。

#2


0  

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

如果所有尝试将chromedriver放入路径失败,也可以将可执行路径交给webdriver.Chrome():

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.

这是我试图从virtualenv运行chromedriver时的最终解决方案。

#1


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).

不确定这是最好的办法,但我发现事情似乎工作:我已经添加到我的翻译已经可用的变量命名的道路,我编辑变量的值,包括我的项目的相对路径的虚拟环境的bin目录(即我chromedriver可执行文件的目录保存)。

More precisely:

更准确地说应该是:

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

    窗口菜单在Eclipse >首选项> PyDev的左侧>解释器> Python解释器。

  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

    从Libaries切换到Preferences窗口的下半部分的环境

  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.

    翻遍列表,发现了一条名为PATH的路径。选择它并单击OK按钮。

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

    它和它的价值(/usr/local/bin:/usr/bin:/ usr/local/games:/usr/games:/usr/local/ bin:/usr/bin:/bin:/ bin:/usr:/ local/games)出现在其他的空列表中。

  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).

    在值的右边,我添加了:env/bin(即从保存我的脚本的目录到项目虚拟环境的bin目录的相对路径)。

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

    单击OK回到Preferences窗口>应用于Preferences窗口> OK关闭Preferences窗口。

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

    在Eclipse中运行程序(选择脚本文件> Run菜单>作为> Python运行)。

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

一个Chrome(哦,铬——这是Debian)窗口打开了,就像我在命令提示符上运行程序时一样。

Great.

太好了。

#2


0  

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

如果所有尝试将chromedriver放入路径失败,也可以将可执行路径交给webdriver.Chrome():

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.

这是我试图从virtualenv运行chromedriver时的最终解决方案。