centos7 安装Chrome和selenium

1 安装Chrome

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
which google-chrome-stable
ln -s /usr/bin/google-chrome-stable /bin/chrome

2 安装 chromedriver

查看chrome版本

chrome --version

找到对应版本的( http://npm.taobao.org/mirrors/chromedriver )下载安装

wget http://npm.taobao.org/mirrors/chromedriver/78.0.3904.105/chromedriver_linux64.zip
unzip chromedriver_linux64.zip 
mv chromedriver /usr/local/bin/
chmod +x /usr/local/bin/chromedriver 

3 安装python3.7( https://www.python.org/downloads/ )选择需要的版本

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install libffi-devel -y

wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz

tar -xzvf Python-3.7.5.tgz 
cd Python-3.7.5
./configure --prefix=/usr/local/python3 --with-ssl
./configure --enable-optimizations
make
make install
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
python3 -V
pip3 -V

4 安装selenium

pip3 install selenium

5 测试是否安装成功,新建test.py

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_options = Options()
   
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument('--headless')
  
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.baidu.com')
print(driver.page_source)
print(driver.title)

执行一下,看是否输出百度官网的名称(“百度一下,你就知道”),说明安装成功

6 牢记 Chrome 和 chromedriver 的版本一定要对应。

发表评论

电子邮件地址不会被公开。 必填项已用*标注