centos7下使用selenium进行网页自动化访问
1、yum安装chrome浏览器
配置yum源
在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo
cd /ect/yum.repos.d/
vim google-chrome.repo
写入如下内容:
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
安装google chrome浏览器:
yum -y install google-chrome-stable
Google官方源可能在中国无法使用,导致安装失败或者在国内无法更新,可以添加以下参数来安装:
yum -y install google-chrome-stable --nogpgcheck

找到chrome路径,并做个软连接,方便使用:
which google-chrome-stable
ln -s xxx /bin/chrome
查看chrome版本:chrome -version
2、或者安装chromium:
注: Chromium 是 Google 的Chrome浏览器背后的引擎,其目的是为了创建一个安全、稳定和快速的通用浏览器。 Chromium是Google为发展自家的浏览器Google Chrome(以下简称Chrome)而开启的计划,所以Chromium相当于Chrome的工程版或称实验版(尽管Chrome自身也有β版阶段),新功能会率先在Chromium上实现,待验证后才会应用在Chrome上,故Chrome的功能会相对落后、稳定。Chromium的更新速度很快,每隔数小时即有新的开发版本发布,而且可以免安装,下载zip封装版后解压缩即可使用(Windows下也有安装版)。Chrome虽然理论上也可以免安装,但Google仅提供安装版。
安装chromium
yum -y install epel-release
yum -y install chromium
chromium-browser --version #查看版本
3、安装chromedriver
在http://chromedriver.storage.googleapis.com/index.html根据你安装的chrome版本查找对应的chromedriver的版本
cd /usr/local/src/ # 进入到软件要下载的目录
wget -c http://chromedriver.storage.googleapis.com/79.0.3945.36/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
4、 写python脚本测试下(脚本文件名testselenium.py,内容如下)
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome( \
chrome_options=chrome_options, \
service_args=['--verbose', '--log-path=chromedriver.log'])
driver.get("https://www.baidu.com/")
time.sleep(1)
print(driver.current_url)
cookie = driver.get_cookies()
print(cookie)
driver.quit()
执行测试 :
