无法在Selenium中使用Chrome驱动
我在使用Selenium的Chrome驱动时遇到了问题。我已经下载了chromedriver,并把它保存在C:\Chrome这个文件夹里:
driver = webdriver.Chrome(executable_path="C:/Chrome/")
但是使用它时出现了以下错误:
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
如果有人能帮忙就太好了。
12 个回答
对于Windows系统
你需要从下面的链接下载webdriver:
http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
下载后,把里面的chromedriver.exe文件放到 "C:\Python27\Scripts" 文件夹里。
这样就应该可以正常使用了。
from selenium import webdriver
driver = webdriver.Chrome()
对于Linux系统
首先,检查一下你是否安装了最新版本的Chrome浏览器,输入"chromium-browser -version"来查看版本号。
如果没有安装最新版本,可以通过"sudo apt-get install chromium-browser"来安装。
接下来,从以下链接获取合适版本的Chrome驱动:
最新版本请访问 https://googlechromelabs.github.io/chrome-for-testing
旧版本请访问 http://chromedriver.storage.googleapis.com/index.html下载后,解压缩chromedriver.zip文件。
然后,把解压后的文件移动到/usr/bin目录,使用sudo mv chromedriver /usr/bin命令。
接下来,进入/usr/bin目录,你需要运行类似于"chmod a+x chromedriver"的命令来设置文件为可执行。
最后,你就可以执行代码了。
代码示例:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
display.stop()
你应该指定可执行文件的完整路径,而不是包含这个可执行文件的文件夹路径。
driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")