Ubuntu 無界面使用selenium chrome + headless

1. 安裝 selenium : sudo pip install seleniumphp

2. 安裝 chromdriver: 進入 淘寶鏡像源 下載 chromdriver, 能夠查看 notes.txt 文件,看chrome 和ChromDriver 二者相對應的兼容版本python

3. 下載chrome : 下載 chromedriver_linux64.zip linux

4. 將 chromdriver 文件放到線上服務器 /usr/bin/ 下 ,將google-chrome-stable_current_amd64.deb上傳線上服務器web

5. 安裝chrome,執行以下命令: chrome

sudo dpkg -i google-chrome*.deb    
sudo apt-get install -f
google-chrome --version      # 查看版本

6. 編寫測試腳本npm

from selenium import webdriver
 
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox') # 這個配置很重要
client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/chromedriver')    # 若是沒有把chromedriver加入到PATH中,就須要指明路徑
 
client.get("https://www.baidu.com")
print (client.page_source.encode('utf-8'))
 
client.quit()
相關文章
相關標籤/搜索