• 欢迎访问金笔头博客,这是一个菜鸟(伪)程序员的自留地,欢迎访问我的github:点击进入

Ubuntu 16.04 下Selenium和Chrome以及Chromedrive的配置

python eason 6332次浏览 0个评论 扫描二维码

下载并安装Chrome

$  wget http://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
--2017-07-18 10:11:00--  http://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
正在连接 192.168.1.9:1080... 已连接。
已发出 Proxy 请求,正在等待回应... 200 OK
长度: 60833124 (58M) [application/x-debian-package]
正在保存至: “google-chrome-stable_current_amd64.deb”

ble_current_amd64.d  13%[=>                  ]   7.84M   356KB/s    eta 3m 24s 

$ sudo dpkg -i google-chrome-stable_current_amd64.deb 
(正在读取数据库 ... 系统当前共安装有 296115 个文件和目录。)
正准备解包 google-chrome-stable_current_amd64.deb  ...
正在将 google-chrome-stable (59.0.3071.115-1) 解包到 (59.0.3071.115-1) 上 ...
正在设置 google-chrome-stable (59.0.3071.115-1) ...
正在处理用于 gnome-menus (3.13.3-6ubuntu3.1) 的触发器 ...
正在处理用于 desktop-file-utils (0.22-1ubuntu5) 的触发器 ...
正在处理用于 bamfdaemon (0.5.3~bzr0+16.04.20160824-0ubuntu1) 的触发器 ...
Rebuilding /usr/share/applications/bamf-2.index...
正在处理用于 mime-support (3.59ubuntu1) 的触发器 ...
正在处理用于 man-db (2.7.5-1) 的触发器 ...

下载Chromedrive

在下载Chromedrive之前我们先要知道Chrome的版本,Ubuntu上打开Chrome浏览器,通过”自定义及控制Google Chrome”->”帮助”->”关于Google Chrome(G)”可知本机Chrome版本信息版本 59.0.3071.115(正式版本) (64 位),在https://chromedriver.storage.googleapis.com/2.30/notes.txt中找到本机Chrome版本所对应的Chromedrive版本。

----------ChromeDriver v2.30 (2017-06-07)----------
Supports Chrome v58-60
Resolved issue 644: ChromeDriver does not delete the profile & scoped_dir* folders after test exits [['Pri-1']]
Resolved issue 1114: unknown error:Maximum call stack size exceeded on calling any webdriver command [['OS-All', 'Pri-1']]
Resolved issue 1773: ChromeDriver crashes when using console.time on Chromev58+ [['OS-All', 'Pri-1']]
Resolved issue 1526: mobileEmulation not working for the listed devices in the chrome [['Pri-2']]
Resolved issue 1777: NoSuchFrameException is thrown when attempting to focus on a frame with protocol "chrome-extension://" within a page with protocol "https://" on Chrome 58 [['OS-All', 'Pri-2']]
Resolved issue 1779: testWindowMaximize is failing on Mac for Chrome v60+ [['OS-Mac', 'Pri-2']]
Resolved issue 1775: testShouldHandleNewWindowLoadingProperly failing on Chrome v60+ [['OS-All', 'Pri-2']]
Resolved issue 1796: Chromedriver failed to connect to Chrome on CrOS with latest canary build [[]]
Resolved issue 1353: Sending text to Alert / Confirm dialog should throw element not visible exception [[]]

----------ChromeDriver v2.29 (2017-04-04)----------
Supports Chrome v56-58
Resolved issue 1521: Assignment to Object.prototype.$family causes a crash [['Pri-1']]
Resolved issue 1482: Chromedriver cannot handle the alert generated by onbeforeunload event [['OS-All', 'Pri-2']]
Resolved issue 1315: |switch_to.window| does not visually switch tabs [['OS-All', 'Pri-3']]

----------ChromeDriver v2.28 (2017-03-09)----------
Supports Chrome v55-57
Resolved issue 1625: Error: cannot get automation extension on Chrome57+ [['OS-All', 'Pri-1', 'merge-merged-2987']]
Resolved issue 1695: Chrome crashes on Linux32 bot [['OS-Linux', 'Pri-1']]
Resolved issue 1467: A hang occurs when an alert dialog is displayed on Chrome 52+ [['OS-All', 'Pri-2']]
Resolved issue 1688: Exported Netlog with --log-net-log is truncated [['OS-Linux', 'OS-Mac', 'Pri-2']]
Resolved issue 1388: find Elements is failing [['Pri-2']]

----------ChromeDriver v2.27 (2016-12-23)----------
Supports Chrome v54-56
Resolved issue 1637: Getting window handles fails in the presence of shared workers on Chrome 55+ [['OS-All', 'Pri-1']]

----------ChromeDriver v2.26 (2016-12-09)----------
Supports Chrome v53-55
Resolved issue 1607: Chrome 57+ with perf logs enabled fails with "unexpected command response" [['OS-All', 'Pri-1']]
Resolved issue 1502: sendKeys(): SPACE key does not work on Chrome 53 [['OS-All', 'Pri-1']]
Resolved issue 984: Extend ChromeDriver capabilities to cover network throttling feature from Chrome DevTools [['OS-All', 'Pri-2']]
Resolved issue 877: UnhandledAlertException not being propagated upwards [['OS-All', 'Pri-2']]
Resolved issue 1578: Flash plugin blocked after updating Chrome browser [['OS-All', 'Pri-2']]
Resolved issue 1617: Fullscreen command is not recognised [['OS-All', 'Pri-3']]
Resolved issue 839: clear does not work on input type number with string value [['Pri-3']]
Resolved issue 1500: "Could not handle JavaScript dialog" error occurs randomly when accepting confirmation dialogs. [[]]
Resolved issue 1541: find element with empty tag is raising wrong exception in findElement [[]]
Resolved issue 1540: empty tagname value should raise an error when doing findElements [[]]

----------ChromeDriver v2.25 (2016-10-25)----------
Supports Chrome v53-55
Resolved issue 1547: Chromedriver crashes during event Runtime.consoleAPICalled [['OS-All', 'Pri-1']]
Resolved issue 1514: GetLog command times out if an alert is showing [['OS-All', 'Pri-1']]
Resolved issue 1460: "Disable Developer Mode Extensions" exists on Mac, but not Windows [[]]

----------ChromeDriver v2.24 (2016-09-09)----------
Supports Chrome v52-54
Resolved issue 1497: GetLog fails when the current window is closed [['OS-All', 'Pri-0']]
Resolved issue 1495: ChromeDriver crashes with "Check failed: !page_load_strategy_.empty()" [['OS-All', 'Pri-0']]
Resolved issue 1463: SessionNotCreatedException: Runtime.evaluate missing 'wasThrown' on Chrome 54+ [['OS-All', 'Pri-0']]
Resolved issue 1484: SendKeys of a Tab Key has no effect in Chrome 53 [['OS-All', 'Pri-1']]
Resolved issue 1431: GetLog command does not work for Chrome 54+ [['OS-All', 'Pri-1']]
Resolved issue 1411: sendKeys generates events that are missing some fields [['OS-All', 'Pri-1']]
Resolved issue 1451: Chrome 54+ session not created exception: Runtime.executionContextCreated has invalid 'context' [['Pri-1']]
Resolved issue 984: Extend ChromeDriver capabilities to cover network throttling feature from Chrome DevTools [['OS-All', 'Pri-2']]
Resolved issue 1454: Net::ReadTimeout error on launching Canary v54 through RemoteWebDriver [[]]

----------ChromeDriver v2.23 (2016-08-04)----------
Supports Chrome v51-53
Resolved issue 1378: Android 6: Intitialization of Chrome driver fails when Chrome/Webview process is in running (R) state [['OS-Android', 'Pri-1']]
Resolved issue 1379: Chromedriver does not catch all available browser console log entries [['OS-All', 'Pri-2']]

----------ChromeDriver v2.22 (2016-06-06)----------
Supports Chrome v49-52
Resolved issue 1348: Timeout error while navigating to URL on Chrome 51+ [['OS-All', 'Pri-0']]
Resolved issue 1381: Timeout error occurs when alert dialog is displayed on Chrome 52+ [['OS-All', 'Pri-1', 'merge-merged-2743']]
Resolved issue 1339: Failure when executing JS in a content script context [['OS-All', 'Pri-1']]
Resolved issue 1387: ChromeDriver hangs when calling driver.get() for same-process navigations [['OS-All', 'Pri-1']]
Resolved issue 1365: Touch emulation is not working under mobile emulation in Chrome 50+ [['OS-All', 'Pri-1']]
Resolved issue 1224: Chrome WebDriver throws an exception when trying to click a button inside a frame  [['OS-All', 'Pri-2']]
Resolved issue 107: ChromeDriver Won't Switch To Frame inside nested frameset [['OS-All', 'Pri-2']]
Resolved issue 1368: ExecuteScript returns "Cannot read property 'document' of null" [[]]
Resolved issue 1355: Session not created exception when connecting to existing browser on localhost [[]]
Resolved issue 1331: "chromeOptions.excludeSwitches" is not working on Android [[]]

----------ChromeDriver v2.21 (2016-01-28)----------
Supports Chrome v46-50
Resolved issue 1276: empty FindElement response is returned while trying to find element with tagname object/embed [['OS-All', 'Pri-1']]
Resolved issue 1272: GET url returns "data:text/html,chromewebdata" [['OS-All', 'Pri-2']]
Resolved issue 1310: ChromeDriver hangs (and times out) when inspecting inactive background pages [['OS-All', 'Pri-2']]
Resolved issue 824: ChromeDriver creates two cookies when the cookie to add contains the domain [['OS-All', 'Pri-2']]
Resolved issue 1274: ExecuteScript sends script to wrong execution context [['OS-All', 'Pri-2']]
Resolved issue 1249: Change "get current URL" behavior in ChromeDriver [['OS-All', 'Pri-2']]
Resolved issue 904: include Alert text in UnhandledAlertError stack trace [['OS-All', 'Pri-2']]
Resolved issue 474: getAttribute() fails when attribute name has colon (:) character [['OS-All', 'Pri-3']]
Resolved issue 1245: Unable to switch to 1st opened window in session [[]]
Resolved issue 1290: Cannot access elements from subframes [[]]
Resolved issue 1261: getTitle() should return empty string instead of page url when page title is empty [[]]
Resolved issue 1238: testExecuteInRemovedFrame fails on waterfall bots [['OS-All']]
Resolved issue 1246: Chromedriver version number is not displayed in logs/ getCapabilities() [['OS-All']]

----------ChromeDriver v2.20 (2015-10-08)----------
Supports Chrome v43-48
Resolved issue 1225: RESPONSE InitSession unknown error: Device <device_id> is not online [['OS-Android', 'Pri-0']]
Resolved issue 960: ChromeDriver support to access WebView elements [['OS-All', 'Pri-2']]
Resolved issue 1229: Spec violation: /sessions endpoint returns session ID at `sessionId` key instead of `id` key [['OS-All', 'Pri-2']]

----------ChromeDriver v2.19 (2015-08-28)----------
Supports Chrome v43-47
Resolved issue 1167: Various issues with ChromeDriver navigation tracker [['OS-All', 'Pri-1']]
Resolved issue 1205: Chrome crashes when enabling touch emulation [['OS-Mac', 'OS-Windows', 'Pri-1']]
Resolved issue 1142: cookies' httponly attribute is not returned [['Pri-2']]

----------ChromeDriver v2.18 (2015-08-19)----------
Supports Chrome v43-46
Resolved issue 1158: Unable to find elements after installing Chrome 44 [['ChromeDriver-2.17', 'OS-All', 'Pri-1']]
Resolved issue 635: Implement touch actions for android chrome [['OS-Android', 'Pri-2']]
Resolved issue 1194: unknown error: cannot determine loading status from disconnected: received Inspector.detached event [[]]

Chromedrive各版本的地址是http://chromedriver.storage.googleapis.com/index.html,按照对应关系,本机应下载ChromeDriver v2.30,并且将chromedrive文件放到以下目录/usr/bin,另外由于本机是64位ubuntu,所以要下载chromedriver_linux64.zip

$ cd /usr/bin/
/usr/bin$ sudo wget http://chromedriver.storage.googleapis.com/2.30/chromedriver_linux64.zip
/usr/bin$ sudo unzip chromedriver_linux64.zip

检查chromedriver_linux64.zip是否解压成功

/usr/bin$ ll | grep chromedrive
-rwxr-xr-x  1 root root     8475456 Jun  7 15:53 chromedriver*
-rw-r--r--  1 root root     3864914 Jun  8 06:53 chromedriver_linux64.zip

可以看到chromedriver已经存在了,接下买可以删掉chromedriver_linux64.zip了

/usr/bin$ sudo rm -f chromedriver_linux64.zip

在虚拟环境中安装最新版本的Selenium

激活本机虚拟环境env_scrapy

$ source ~/env_scrapy/bin/activate
$ pip install -U selenium
Collecting selenium
  Downloading selenium-3.4.3-py2.py3-none-any.whl (931kB)
    100% |████████████████████████████████| 942kB 96kB/s 
Installing collected packages: selenium
Successfully installed selenium-3.4.3

执行Python脚本测试

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.jinbitou.net")
time.sleep(5)
browser.quit()

如果出现提示Chrome正收到自动测试软件的控制,修改成如下代码就OK了

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('disable-infobars')
browser = webdriver.Chrome(chrome_options=options)
browser.get("http://jinbitou.net")
time.sleep(5)
browser.quit()

金笔头博客, 版权所有丨如未注明 , 均为原创, 转载请注明Ubuntu 16.04 下Selenium和Chrome以及Chromedrive的配置
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址