当前位置:首页 - Linux

CentOS Python3 Selenium+ChromeDriver 安装步骤

作者:高景洋 日期:2020-03-23 19:25:33 浏览次数:2001

1、安装Chrome

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


2、安装Selenium

pip3 install selenium


3、下载ChromeDriver (注意:chromedriver 的大版本,要与Chrome 的大版本一致)

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


4、解压chromedriver

unzip chromedriver_linux64.zip


5、将解压后的chromedriver 复制到 /user/bin 下

cp /root/chromedriver /usr/bin/chromedriver


6、给文件,增加可执行权限


chmod +x /usr/bin/chromedriver


7、安装xvfb

yum install Xvfb -y


8、yum install xorg-x11-fonts* -y


9、新建 文件vi /usr/bin/xvfb-chrome

内容为


#!/bin/bash
_kill_procs() {  
  kill -TERM $chrome  
  wait $chrome  
  kill -TERM $xvfb  
}  
 
# Setup a trap to catch SIGTERM and relay it to child processes  
trap _kill_procs SIGTERM  
 
XVFB_WHD=${XVFB_WHD:-1280x720x16}  
 
# Start Xvfb  
Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &  
xvfb=$!  
 
export DISPLAY=:99  
 
chrome --no-sandbox --disable-gpu$@ &  
chrome=$!  
 
wait $chrome  
wait $xvfb


10、给刚创建的文件,增加可执行权限

chmod +x /usr/bin/xvfb-chrome


11、查看当前,映射关系

ll /usr/bin/ | grep chrom


12、建立软链

ln -s /etc/alternatives/google-chrome /usr/bin/chrome  
rm -rf /usr/bin/google-chrome 
ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome


13、查看 修改后的映射关系

ll /usr/bin/ | grep chrom


14、安装完成


15、代码 test

------------------------------


from selenium import webdriver


options = webdriver.ChromeOptions()

options.add_argument('--headless')

options.add_argument('--no-sandbox') # [MARK]This argument must be exist.

driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)

driver.get('http://py2app.readthedocs.io/en/latest/examples.html')

print(driver.title)


本文永久性链接:
<a href="http://r4.com.cn/art135.aspx">CentOS Python3 Selenium+ChromeDriver 安装步骤</a>
当前header:Host: r4.com.cn X-Host1: r4.com.cn X-Host2: r4.com.cn X-Host3: 127.0.0.1:8080 X-Forwarded-For: 18.117.91.116 X-Real-Ip: 18.117.91.116 X-Domain: r4.com.cn X-Request: GET /art135.aspx HTTP/1.1 X-Request-Uri: /art135.aspx Connection: close Accept: */* User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com) Accept-Encoding: gzip, br, zstd, deflate