溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于linux配置selenium環(huán)境的方法

發(fā)布時間:2020-08-21 09:55:48 來源:億速云 閱讀:340 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹基于linux配置selenium環(huán)境的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、在linux中使用selenium

1、安裝chrome

用下面的命令安裝Google Chrome

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

也可以先下載至本地,然后安裝

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install ./google-chrome-stable_current_x86_64.rpm

安裝必要的庫

yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts

2、安裝 chromedriver(末尾附chrome和chromedriver的對應(yīng)版本)

chrome官網(wǎng)

wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip

淘寶源(推薦)

wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip

將下載的文件解壓,放在如下位置

unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/

給予執(zhí)行權(quán)限

chmod +x /usr/bin/chromedriver

3、運(yùn)行代碼,查看是否成功(python下)

from selenium import webdriver
driver = webdriver.Chrome()

------------2019年兼容版本對照表-----------
ChromeDriver 78.0.3904.11 (2019-09-12)---------Supports Chrome version 78
ChromeDriver 77.0.3865.40 (2019-08-20)---------Supports Chrome version 77
ChromeDriver 76.0.3809.12 (2019-06-07)---------Supports Chrome version 76
ChromeDriver 75.0.3770.8 (2019-04-29)---------Supports Chrome version 75
ChromeDriver v74.0.3729.6 (2019-03-14)--------Supports Chrome v74
ChromeDriver v2.46 (2019-02-01)----------Supports Chrome v71-73

二、chrome無界面模式運(yùn)行

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')#解決DevToolsActivePort文件不存在的報錯
chrome_options.add_argument('window-size=1920x3000') #指定瀏覽器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文檔提到需要加上這個屬性來規(guī)避bug
chrome_options.add_argument('--hide-scrollbars') #隱藏滾動條, 應(yīng)對一些特殊頁面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加載圖片, 提升速度
chrome_options.add_argument('--headless') #瀏覽器不提供可視化頁面. linux下如果系統(tǒng)不支持可視化不加這條會啟動失敗
 
#創(chuàng)建瀏覽器對象
driver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)#executable_path:瀏覽器驅(qū)動路徑
driver.get(url)

三、無界面模式下下載文件

以前,以無頭模式運(yùn)行的Chromedriver無法正確下載文件,原因是它稀疏地解析提供給它的首選項(xiàng)文件。無頭Chrome團(tuán)隊(duì)的工程師建議使用DevTools的“ Page.setDownloadBehavior”來解決此問題。此變更列表實(shí)現(xiàn)此修復(fù)程序。下載的文件默認(rèn)為當(dāng)前目錄,可以在實(shí)例化chromedriver實(shí)例時使用download_dir進(jìn)行設(shè)置。還添加了測試以確保正確的下載功能。

params = {'behavior': 'allow', 'downloadPath': r'C:\Users\Debanjan.B\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)

以上是基于linux配置selenium環(huán)境的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI