可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using selenium with python and have downloaded the chromedriver for my windows computer from this site: http://chromedriver.storage.googleapis.com/index.html?path=2.15/
After downloading the zip file, I unpacked the zip file to my downloads folder. Then I put the path to the executable binary (C:\\Users\\michael\\Downloads\\chromedriver_win32) into the Environment Variable \"Path\".
However, when I run the following code:
from selenium import webdriver
driver = webdriver.Chrome()
... I keep getting the following error message:
WebDriverException: Message: \'chromedriver\' executable needs to be available in the path. Please look at http://docs.seleniumhq.org/download/#thirdPartyDrivers and read up at http://code.google.com/p/selenium/wiki/ChromeDriver
But - as explained above - the executable is(!) in the path ... what is going on here?
回答1:
You can test if it actually is in the PATH, if you open a cmd and type in chromedriver
(assuming your chromedriver executable is still named like this) and hit Enter. If Starting ChromeDriver 2.15.322448
is appearing, the PATH is set appropriately and there is something else going wrong.
Alternatively you can use a direct path to the chromedriver like this:
driver = webdriver.Chrome(\'/path/to/chromedriver\')
So in your specific case:
driver = webdriver.Chrome(\"C:/Users/michael/Downloads/chromedriver_win32/chromedriver.exe\")
回答2:
We have to add path string, begin with the letter r
before the string, for raw string. I tested this way, and it works.
driver = webdriver.Chrome(r\"C:/Users/michael/Downloads/chromedriver_win32/chromedriver.exe\")
回答3:
Same situation with pycharm community edition, so, as for cmd, you must restart your ide in order to reload path variables. Restart your ide and it should be fine.
回答4:
I see the discussions still talk about the old way of setting up chromedriver by downloading the binary and configuring path manually.
This can be done automaticatically using webdriver-manager
pip install webdriver-manager
Now the above code in the question will work simply with below change,
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
The same can be used to set Firefox, Edge and ie binaries
回答5:
When you unzip chromedriver, please do specify an exact location so that you can trace it later. Below, you are getting the right chromedriver for your OS, and then unzipping it to an exact location, which could be provided as argument later on in your code.
wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip
unzip chromedriver_linux64.zip -d /home/virtualenv/python2.7.9/
回答6:
Some additional input/clarification for future readers of this thread,
to avoid tinkering with the PATH env. variable at the Windows level and restart of the Windows system:
(copy of my answer from https://stackoverflow.com/a/49851498/9083077 as applicable to Chrome):
(1) Download chromedriver (as described in this thread earlier) and place the (unzipped) chromedriver.exe at X:\\Folder\\of\\your\\choice
(2) Python code sample:
import os;
os.environ[\"PATH\"] += os.pathsep + r\'X:\\Folder\\of\\your\\choice\';
from selenium import webdriver;
browser = webdriver.Chrome();
browser.get(\'http://localhost:8000\')
assert \'Django\' in browser.title
Notes:
(1) It may take about 5 seconds for the sample code (in the referenced answer) to open up the Firefox browser for the specified url.
(2) The python console would show the following error if there\'s no server already running at the specified url or serving a page with the title containing the string \'Django\':
assert \'Django\' in browser.title
AssertionError
回答7:
Could try to restart computer if it doesn\'t work after you are quite sure that PATH is set correctly.
In my case on windows 7, I always got the error on WebDriverException: Message: for chromedriver, gecodriver, IEDriverServer. I am pretty sure that i have correct path. Restart computer, all work
回答8:
If you are working with robot framework RIDE. Then you can download Chromedriver.exe
from its official website and keep this .exe file in C:\\Python27\\Scripts
directory. Now mention this path as your environment variable eg. C:\\Python27\\Scripts\\chromedriver.exe
.
Restart your computer and run same test case again. You will not get this problem again.
回答9:
In my case, this error disappears when I have copied chromedriver file to c:\\Windows folder. Its because windows directory is in the path which python script check for chromedriver availability.
回答10:
According to the instruction, you need to include the path to ChromeDriver when instantiating webdriver.Chrome eg.:
driver = webdriver.Chrome(\'/path/to/chromedriver\')
回答11:
(for Mac users)
I have the same problem but i solved by this simple way:
You have to put your chromedriver.exe in the same folder to your executed script and than in pyhton write this instruction :
import os
os.environ[\"PATH\"] += os.pathsep + r\'X:/your/folder/script/\'