安装了python selenium,运行下面代码:
1 from selenium import webdriver 2 3 browser = webdriver.Chrome() 4 browser.get('http://www.baidu.com/') # 启动Chrome浏览器打开百度网址
报错:
1 Traceback (most recent call last): 2 File "I:/python/c ip/go.py", line 4, in <module> 3 browser.get('http://www.baidu.com/') 4 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdriver emotewebdriver.py", line 324, in get 5 self.execute(Command.GET, {'url': url}) 6 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdriver emotewebdriver.py", line 310, in execute 7 response = self.command_executor.execute(driver_command, params) 8 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdriver emote emote_connection.py", line 466, in execute 9 return self._request(command_info[0], url, body=data) 10 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libsite-packagesseleniumwebdriver emote emote_connection.py", line 490, in _request 11 resp = self._conn.getresponse() 12 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libhttpclient.py", line 1174, in getresponse 13 response.begin() 14 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libhttpclient.py", line 282, in begin 15 version, status, reason = self._read_status() 16 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libhttpclient.py", line 243, in _read_status 17 line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") 18 File "C:UsersAdministratorAppDataLocalProgramsPythonPython35-32libsocket.py", line 571, in readinto 19 return self._sock.recv_into(b) 20 ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
并且Chrome浏览器提示:请停用以开发者模式运行的扩展程序,网址输入框中只显示:data;
,如下图:
这个问题是由于因为浏览器驱动Chromedriver和Chrome的版本不兼容导致无法通过driver在浏览器输入网址而出现的,解决的办法很简单:查看你Chrome浏览器的版本(帮助 -> 关于Google Chrome(G)),下载对应版本的浏览器驱动就可以了。
下载对应Chromedriver后放到python目录下,成功运行程序,结果如下:
官网上对Chromedriver支持Chrome版本的声明,部分截图如下:
附表:最新的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+Chrome做自动化测试的时候一定安装和下载版本相匹配的Chromedriver和Chrome,不然会遇到各种各样的兼容问题。
不同版本的Chromedriver都可以通过这里链接下载: