安装了selenium,python运行下面代码:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/') # 启动Chrome浏览器打开百度网址
报错:
Traceback (most recent call last):
File "I:/python/c ip/go.py", line 4, in
browser.get('http://www.baidu.com/')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 324, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 310, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 466, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 490, in _request
resp = self._conn.getresponse()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1174, in getresponse
response.begin()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 282, in begin
version, status, reason = self._read_status()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 243, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 571, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
并且Chrome浏览器提示:请停用以开发者模式运行的扩展程序,网址输入框中只显示:data;
,如下图:
这个问题是由于因为浏览器驱动Chromedriver和Chrome的版本不兼容导致无法通过driver在浏览器输入网址而出现的,解决的办法很简单:查看你Chrome浏览器的版本(帮助 -> 关于Google Chrome(G)),下载对应版本的浏览器驱动就可以了。
下载对应Chromedriver后放到python目录下,成功运行程序,结果如下:
附表:最新的Chromedriver与Chrome的版本对应关系表:
chromedriver版本 | 支持的Chrome版本 |
---|---|
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
不同版本的Chromedriver都可以通过这里链接下载:
http://chromedriver.storage.googleapis.com/index.html