1. 程式人生 > >ubuntu上安裝及使用selenium和google-chrome和chromedriver

ubuntu上安裝及使用selenium和google-chrome和chromedriver

環境:

系統:Ubuntu16.04x64位,使用的是租用的虛擬主機

安裝selenium:

pip install selenium

安裝google-chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb

如果報錯缺乏依賴,執行以下語句:

sudo apt-get install -f

在安裝Chromedriver之前,注意版本:

各版本對應地址:

https://blog.csdn.net/suancai1993/article/details/79742852

驅動 對應瀏覽器版本
2.42 v68-70
2.41 v67-69
2.40 v66-68
2.39 v66-68
2.38 v65-67
2.37 v64-66
2.36 v63-65
2.35 v62-64
2.34 v61-63
2.33 v60-62
2.32 v59-61
2.31 v58-60
2.30 v58-60
2.29 v56-58

進入檢視版本的對應:http://chromedriver.storage.googleapis.com/2.31/notes.txt

進入此頁面選擇下載Chromedriver:

http://chromedriver.storage.googleapis.com/index.html

下載並安裝chromedriver:

wget https://chromedriver.storage.googleapis.com/2.31/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv chromedriver /usr/bin/

編寫測試指令碼:

#-*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
print(driver.title)       

報錯:

selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

解決辦法:

sudo apt-get install libnss3-dev

發現已經安裝,執行chromedriver,報錯:

chromedriver: error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory

執行如下安裝命令:

sudo apt-get install libgconf-2-4

繼續執行chromedriver,出現以下則為正常:

Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 9515
Only local connections are allowed.

再次執行指令碼出現以下,則成功完成.

百度一下,你就知道

自己安裝的時候主要的問題是缺少libgconf-2.so.4依賴,看到一篇部落格中記錄的解決方案是直接安裝libgconf-2-4.

安裝完之後還是出現了版本不對應的問題。報錯: