centos7無GUI運行selenium chromedriver 親測可用!


1. 安裝chrome

首先安裝google的epel源

vi /etc/yum.repos.d/google.repo
[google]
name=Google-x86_64
baseurl=http://dl.google.com/linux/rpm/stable/x86_64
enabled=1
gpgcheck=0
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub 

yum update , 然后yum install google-chrome-stable

2. chromedriver下載

https://npm.taobao.org/mirrors/chromedriver/

找到chrome對應的chromedriver 版本,並下載

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

將下載的chromedriver 放到腳本同級目錄調用

3 . 為chromedriver授權

chmod 755 chromedriver

4. 測試代碼 ts.py

from selenium import webdriver
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('--no-sandbox')    # 禁止沙箱模式,否則肯能會報錯遇到chrome異常
url="https://www.west.cn/login.asp"
brower=webdriver.Chrome(executable_path="./chromedriver", chrome_options=chrome_options)
brower.get(url)
print(brower.current_url)
brower.get("https://www.west.cn/Manager/")
print(brower.current_url)
brower.quit()

5. 測試結果

參考鏈接:

centos 7 無gui界面運行selenium + chrome模擬登陸

在CentOS7上使用chrome(selenium)

selenium操作chrome時的一些配置

【Linux】CentOS7上解壓zip需要安裝uzip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM