【已解決】Ubuntu19.04部署Python selenium+Chrome時遇到的各種坑

一、安裝selenium

pip3 install selenium

二、安裝Chrome

  • 1、下載Chrome包,注意Ubuntu系統上要下載deb格式的文件,如果直接安裝,會下載最新版的chrome,而我們在使用selenium的時候,需要chrome的驅動,最新版驅動比較難找到。
  • 2、chrome各歷史版本下載地址彙總:https://dl.lancdn.com/landian/soft/chrome/m/?utm_sources=DownPageBot
  • 3、我這邊選擇下載 75.0.3770.90_amd64.deb(後面chromedriver驅動要對應版本,實在難找剛好對應的版本啊!!)
  • 4、將下載好的文件上傳到服務器任意路徑上,我這邊是/www/wwwroot,輸入以下命令進行安裝
sudo su  #以root權限登錄,不然後面一直要加sudo
cd /www/wwwroot 
dpkg -i 75.0.3770.90_amd64.deb  

此時,一般會報錯,網上很多方法都試了,說apt-get -f install一下就行,發現不成功
【解決】
(1)解決apt源問題,修改服務器 /etc/apt/sources.list,當然你先備份一份,修改內容如下:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
#測試版源
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
#源碼
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
##測試版源
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
#Canonical 合作伙伴和附加
deb http://archive.canonical.com/ubuntu/ xenial partner
deb http://extras.ubuntu.com/ubuntu/ xenial main

(2)更新源

apt update
apt upgrade
apt-get -f install

(3)修復安裝時broken的依賴

apt --fix-broken install

(4)檢查apt,發現還是出現依賴錯誤

apt-get check 

The following packages have unmet dependencies:
libcairo2 : Depends: libpng12-0 (>= 1.2.13-4) but it is not installed
libgdk-pixbuf2.0-0 : Depends: libpng12-0 (>= 1.2.13-4) but it is not installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
Ubuntu19.04中,可以通過PPA安裝libpng,安裝PPA及libpng12-0的命令如下,輸入以下命令:

add-apt-repository ppa:linuxuprising/libpng12
apt update
apt install libpng12-0

(5)再檢查apt,無相關依賴錯誤的話,重新安裝Chrome,成功安裝

apt-get check 
dpkg -i 75.0.3770.90_amd64.deb
google-chrome --version
返回信息:Google Chrome 75.0.3770.90 表示成功

ps:我選擇安裝75.0.3770.90的版本,是因爲驅動剛好有,若你找不到對應的,可以執行apt-get autoremove google-chrome-stable卸載,回到之前步驟重新安裝

三、部署代碼,嘗試運行selenium

  • 1、遇到 selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path錯誤:
    原因: chromedriver路徑設置的不對
    解決: 將上面下載的chromedriver放對位置,相對定位、絕對定位都行,我放在了項目的上一層目錄,這樣git下來,與本地的chromedriver不會衝突
from selenium import webdriver
	
path = "../chromedriver"
webdriver.Chrome(executable_path=path)

  • 2、遇到 selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
    (Driver info: chromedriver=76.0.3809.12 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.4.27-boot2docker x86_64)錯誤:

    原因: chromedriver的版本和chrom瀏覽器的版本不對應
    解決: 回到二,重新安裝相對應的版本

  • 3、遇到selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
    (unknown error: DevToolsActivePort file doesn't exist)
    (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) 錯誤:

    解決代碼: :

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

def get_chrome_driver():
    path = "../chromedriver"  # 注意這個路徑需要時可執行路徑(chmod 777 dir or 755 dir)
    chrome_options = Options()
    chrome_options.add_argument('window-size=1920x3000')  # 指定瀏覽器分辨率
    chrome_options.add_argument('--disable-gpu')  # 谷歌文檔提到需要加上這個屬性來規避bug
    chrome_options.add_argument('--hide-scrollbars')  # 隱藏滾動條, 應對一些特殊頁面
    chrome_options.add_argument('blink-settings=imagesEnabled=false')  # 不加載圖片, 提升速度
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--headless')

    return webdriver.Chrome(executable_path=path, options=chrome_options)

driver = get_chrome_driver()

四、至此,selenium終於跑起來了,搞了6個小時,入了太多的坑,,最後發現好像也沒有這麼難,一入坑,深似海…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章