阿里云ESC-CentOS7 安装Chrome+Webdriver+Selenium

1. 创建yum源

1
vim /etc/yum.repos.d/google-chrome.repo

1
2
3
4
5
6
7
# 添加如下内容:
[google-chrome]
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

2. 安装google chrome

1
yum -y install google-chrome-stable --nogpgcheck

3. 查看版本和软链接

1
2
3
4
5
[root@CentOS7 ~]# google-chrome --version
Google Chrome 83.0.4103.116
[root@CentOS7 ~]# ll /usr/bin/ | grep chrome
lrwxrwxrwx    1 root root          31 Jul  4 16:34 google-chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx    1 root root          32 Jul  4 16:34 google-chrome-stable -> /opt/google/chrome/google-chrome

此处我 google-chrome 的版本是 83.0.4103.116

4. 安装chromedriver

4.1 下载对应版本的chromedriver

下载地址:http://npm.taobao.org/mirrors/chromedriver/

下载相近的 chromedriver 版本 83.0.4103.14,右键 chromedriver_linux64.zip,复制链接地址就可以了。

1
2
3
4
5
6
7
8
# 创建并进入存放 chromedriver 的路径
mkdir -p ~/download && cd ~/download

# 下载
wget http://npm.taobao.org/mirrors/chromedriver/83.0.4103.14/chromedriver_linux64.zip

# 解压
unzip chromedriver_linux64.zip

4.2 如果原先下载了其他不对应版本的 chromedriver,删除软链接,否则跳过此步骤

1
2
3
4
[root@CentOS7 download]# ll /usr/bin/ | grep chromedriver
lrwxrwxrwx    1 root root          40 Jul  4 16:35 chromedriver -> /usr/lib64/chromium-browser/chromedriver
[root@CentOS7 download]# rm /usr/bin/chromedriver
rm: remove symbolic link ‘/usr/bin/chromedriver’? yes

4.3 配置软链接

1
[root@CentOS7 download]# ln -s ~/download/chromedriver /usr/bin/

4.4 查看版本

1
2
[root@CentOS7 download]# chromedriver --version
ChromeDriver 83.0.4103.14 (be04594a2b8411758b860104bc0a1033417178be-refs/branch-heads/4103@{#119})

5. 安装selenium

1
pip3 install selenium

6. 测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# !/usr/bin/env python
# -*- coding=UTF-8 -*-
# 测试代码
import time
from selenium import webdriver
def test():
    chromeOptions = webdriver.ChromeOptions()

    chromeOptions.add_argument('--headless')  # 浏览器无窗口加载
    chromeOptions.add_argument('--disable-gpu')  # 不开启GPU加速

    """
    解决报错:
    selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
    (unknown error: DevToolsActivePort file doesn't exist)
    """
    chromeOptions.add_argument('--disable-dev-shm-usage')  # 禁止
    chromeOptions.add_argument('--no-sandbox')  # 以根用户打身份运行Chrome,使用-no-sandbox标记重新运行Chrome

    # 其它设置(可选):
    # chromeOptions.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面
    # chromeOptions.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
    # chromeOptions.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")  #伪装其它版本浏览器,有时可以解决代码在不同环境上的兼容问题,或者爬虫cookie有效性保持一致需要设置此参数

    # 创建driver对象
    # chrome_options=chromeOptions加载设置
    # executable_path="/usr/bin/chromedriver"指定webdriver路径(可选)
   
    driver = webdriver.Chrome(chrome_options=chromeOptions, executable_path="/usr/bin/chromedriver")
    try:
        driver.get("http://www.baidu.com")
        time.sleep(3)
        print(driver.page_source)
    except Exception as e:
        print(e)
    finally:
        driver.quit()

if __name__ == '__main__':
    test()