1. 程式人生 > >Ubuntu 下selenium + Chrome 的安裝使用

Ubuntu 下selenium + Chrome 的安裝使用

selenium + Chrome 的

為什麽要使用selenium + Chrome/Firefox.不是大家都用PhantomJS嘛. 我的測試發現不知道是什麽原因,PhantomJS總是無緣無辜的出現bug. 可能是版本不兼容的問題吧.我選擇了selenium + Chrome/Firefox. 有時候使用Chrome時,某些點擊按鈕模擬點擊事會報錯,說element is not clickable.(排除了上面有遮罩的情況),這是使用Firefox是可以的. 所以使用的使用看情況吧

首先:我們在Ubuntu中安裝了CHrome或在FireFox

Ubuntu中安裝CHrome

第一步:
下載deb文件 可以去谷歌官網下載 https://www.google.cn/chrome/

拉到最下面 ‘其他版本下載’

技術分享圖片

第二步:
安裝指令:

sudo dpkg -i google-chrome-stable_current_amd64.deb

安裝過程可能有依賴錯誤.如:
dpkg: 處理軟件包 google-chrome-stable (–install)時出錯:
依賴關系問題 - 仍未被配置

第三步: 更新依賴包

(如果出現第二步中的錯誤,那麽執行這步的指令)

 sudo apt-get -f install

第四步: 查看是否安裝成功

通過whereis google-chrome 查找安裝路徑

fengyan@ubuntu:~/桌面$ whereis google-chrome
google-chrome: /usr/bin/google-chrome      /usr/share/man/man1/google-chrome.1.gz

說明安裝成功.

第五步:

fengyan@ubuntu:~/桌面$  /usr/bin/google-chrome 即可打開

其次:安裝selenium

直接使用 pip install selenium.不再贅述.

再次:下載chromedriver(火狐使用geckodriver)驅動

http://npm.taobao.org/mirrors/chromedriver/這裏下載驅動

技術分享圖片

下載後,將chromedriver文件放到/usr/bin 下

sudo mv chromedriver路徑 /usr/bin

跑起來

from selenium import webdriver

firefox = webdriver.Chrome()

firefox.get(‘https://www.douban.com/‘)
login_xpath = ‘//input[@class="bn-submit"]‘
firefox.find_element_by_xpath(login_xpath).click()

註意: 如果chrome被打開後,出現錯誤

–ignore-certificate-errors.. 原因是:

google-chrom的版本和chromedriver版本不兼容

解決:
chrome://version/ 查看谷歌版本
65.0.3325.162 (正式版本) (64 位)

chromedriver –version
ChromeDriver 2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23)

這兩個版本可以兼容

Ubuntu 下selenium + Chrome 的安裝使用