centos 虚拟机搭建selenium+Chromedriver环境

安装 Chrome

方式一

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

方式二

1
2
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate  
sudo yum install google-chrome-stable_current_x86_64.rpm

查看chrome版本号

1
google-chrome -version

安装chromedriver

提示:chromedriver要和chrome版本一一对应,不然会报错

chromedriver下载地址

1
2
3
4
5
6
7
8
9
10
11
12
13
执行:wget http://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_linux64.zip 
下载安装包(需要确保安装的版本与Chrome匹配兼容)

解压:unzip chromedriver_linux64.zip # 解压zip
解压zip包,如果提示没有zip,那就执行:yum install -y unzip zip 安装zip, unzip)

解压后把chromedriver移动到/usr/bin/目录下

执行:mv chromedriver /usr/bin/

查看chromedriver版本

执行:chromedriver --version

安装selenium

在相应的环境当中运行

1
pip install selenium

运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
# ! -*- coding:utf-8 -*-

from selenium import webdriver

def main():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # 16年之后,chrome给出的解决办法,抢了PhantomJS饭碗
chrome_options.add_argument('--disable-gpu') # root用户不加这条会无法运行
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.baidu.com")
print(driver.page_source)
driver.quit()


if __name__ == '__main__':
main()