Python Selenium 无法使用 Chrome 驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22130109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use chrome driver for Selenium
提问by
I'm having trouble using the Chrome driver for Selenium. I have the chromedriver downloaded and saved to C:\Chrome:
我在使用 Selenium 的 Chrome 驱动程序时遇到问题。我下载了 chromedriver 并保存到 C:\Chrome:
driver = webdriver.Chrome(executable_path="C:/Chrome/")
Using that gives me the following error:
使用它给了我以下错误:
Traceback (most recent call last):
File "C:\Python33\lib\subprocess.py", line 1105, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\service.py", line 63, in start
self.service_args, env=env, stdout=PIPE, stderr=PIPE)
File "C:\Python33\lib\subprocess.py", line 817, in __init__
restore_signals, start_new_session)
File "C:\Python33\lib\subprocess.py", line 1111, in _execute_child
raise WindowsError(*e.args)
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Wilson/Dropbox/xxx.py", line 71, in <module>
driver = webdriver.Chrome(executable_path="C:/Chrome/")
File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
self.service.start()
File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\service.py", line 68, in start
and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path. Please download from http://chromedriver.storage.googleapis.com/index.html
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by falsetru
You should specify the executable file path, not the directory path that contains the executable.
您应该指定可执行文件路径,而不是包含可执行文件的目录路径。
driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")
回答by Prashanth Sams
Chrome
铬合金
import os
from selenium import webdriver
chromedriver = "C://chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver =webdriver.Chrome(chromedriver)
回答by Vicky
For Linux
1. Check you have installed latest version of chrome browser-> "chromium-browser -version"
2. If not, install latest version of chrome "sudo apt-get install chromium-browser"
3. Get the appropriate version of chrome driver from http://chromedriver.storage.googleapis.com/index.html
4. Unzip the chromedriver.zip
5. Move the file to /usr/bin directory sudo mv chromedriver /usr/bin
6. Goto /usr/bin directory and you would need to run something like "chmod a+x chromedriver" to mark it executable.
7. finally you can execute the code.
对于 Linux
1. 检查您是否安装了最新版本的 chrome浏览器 -> “chromium-browser -version”
2. 如果没有,请安装最新版本的 chrome “sudo apt-get installchromium-browser”
3. 获取适当版本的 chrome来自http://chromedriver.storage.googleapis.com/index.html 的驱动程序
4. 解压缩 chromedriver.zip
5. 将文件移动到 /usr/bin 目录sudo mv chromedriver /usr/bin
6. 转到 /usr/bin 目录并且您需要运行诸如“ chmod a+x chromedriver”之类的东西来将其标记为可执行。
7. 终于可以执行代码了。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
display.stop()
回答by Abdul Majeed
For windows
窗户用
Download webdriver from:
从以下位置下载网络驱动程序:
http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
Paste the chromedriver.exe file in "C:\Python27\Scripts" Folder.
将 chromedriver.exe 文件粘贴到“C:\Python27\Scripts”文件夹中。
This should work now.
这现在应该可以工作了。
from selenium import webdriver
driver = webdriver.Chrome()
回答by Daan
In addition to the selected answer (windows style path):
除了选定的答案(Windows 样式路径):
driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")
Note the rin front of the "C:\Chrome\chromedriver.exe", this makes this string a raw string.
请注意“C:\Chrome\chromedriver.exe”前面的r,这使该字符串成为原始字符串。
In case you do not want to use a raw string you should escape the slash like so \\, this would become:
如果您不想使用原始字符串,您应该像这样转义斜杠 \\,这将变成:
driver = webdriver.Chrome(executable_path="C:\Chrome\chromedriver.exe")
Or you can replace the \ with a /, you will get this:
或者你可以用/替换\,你会得到这个:
driver = webdriver.Chrome(executable_path="C:/Chrome/chromedriver.exe")
回答by Harshdeep Singh
When you call selenium or any testing automation library, you would need to add this the code here is in Pythonbut this can be done in Javaand Rubyas well.
当您调用 selenium 或任何测试自动化库时,您需要将其添加到此处的代码中,Python但这也可以在Java和 中完成Ruby。
options = webdriver.ChromeOptions()
options.binary_location = '/usr/bin/chromium-browser'
#All the arguments added for chromium to work on selenium
options.add_argument("--no-sandbox") #This make Chromium reachable
options.add_argument("--no-default-browser-check") #Overrides default choices
options.add_argument("--no-first-run")
options.add_argument("--disable-default-apps")
driver = webdriver.Chrome('/home/travis/virtualenv/python2.7.9/chromedriver',chrome_options=options)
回答by Amit Kumar
Just place the chromedriver.exe in your python folder (in my case: C:\Python27) and use the below mentioned code it will work for you guys
只需将 chromedriver.exe 放在你的 python 文件夹中(在我的例子中:C:\Python27)并使用下面提到的代码,它对你们有用
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("URL")
回答by Shinto Joseph
For Debian/Ubuntu - it works:
对于 Debian/Ubuntu - 它有效:
install Google Chrome for Debian/Ubuntu:
为 Debian/Ubuntu 安装谷歌浏览器:
sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-
stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
sudo apt-get install -f
Install ChromeDriver:
安装 ChromeDriver:
wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
Install Selenium:
安装硒:
pip install -U selenium
Selenium in Python:
Python 中的硒:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.google.co.in/')
回答by Shinto Joseph
All you need to do is Paste the Chromedriver.exein python36-32folder.And you can use It Simply like:
所有你需要做的是粘贴Chromedriver.exe在python36-32folder.And你可以使用它只是这样的:
from selenium import webdriver
driver = webdriver.Chrome()
No need to paste path again and again.
无需一次又一次地粘贴路径。
OR
You can Use:
或者
您可以使用:
driver = webdriver.Chrome(executable_path="C:/Chrome/chromedriver.exe")
回答by venkatesh kotagiri
import os
from selenium import webdriver
chromedriver = "C:\Drivers\chromedriver_win32\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver =webdriver.Chrome(chromedriver)
driver.get("https://www.facebook.com")
print(driver.title)
driver.close()

