(爬蟲、自動化測試、Python)Linux系統安裝chrome與chromedriver

一、安裝chrome瀏覽器

1、安裝依賴
sudo apt-get install libxss1 libappindicator1 libindicator7

2、下載chrome安裝包
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

3、安裝

sudo dpkg -i google-chrome*.deb
sudo apt-get install -f

二、安裝ChromeDriver

1、安裝xvfb

sudo apt-get install xvfb
sudo apt-get install unzip

2、查看chrome版本號

google-chrome --version
//運行結果:Google Chrome 77.0.3865.90

3、下載 對應 安裝包(此處版本號需要參考對照表:2019 Selenium Chrome版本與chromedriver兼容版本對照表)

//  例:
//  當前chrome版本Chrome v77
//  參考對照表,則需要安裝ChromeDriver v77.0.3865.40版本,所以對應的安裝語法如下
wget -N http://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip

4、解壓縮

unzip chromedriver_linux64.zip

5、移動

sudo mv -f chromedriver /usr/local/share/chromedriver

6、建立軟連接

sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

7、安裝python依賴

pip3 install selenium
pip3 install pyvirtualdisplay

8、安裝chromedriver所需依賴

sudo apt -y install libgconf2-4

9、安裝chromedriver

wget -N http://chromedriver.storage.googleapis.com//chromedriver_linux64.zip

10、查看chromedriver版本號,測試是否安裝成功

chromedriver --versin
//  運行結果:ChromeDriver 77.0.3865.10

三、測試運行

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("window-size=1000,800")
chrome_options.add_argument("--no-sandbox")
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('http://www.baidu.com')
print(driver.title)
print('無頭瀏覽器啓動成功')

//打印結果

百度一下,你就知道
無頭瀏覽器啓動成功
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章