1. 创建yum源
vim /etc/yum.repos.d/google-chrome.repo
[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
yum -y
install google-chrome-stable --nogpgcheck
3. 查看版本和软链接
[root@CentOS7 ~
]
Google Chrome 83.0.4103.116
[root@CentOS7 ~
]
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,复制链接地址就可以了。
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,删除软链接,否则跳过此步骤
[root@CentOS7 download
]
lrwxrwxrwx 1 root root 40 Jul 4 16:35 chromedriver -
> /usr/lib64/chromium-browser/chromedriver
[root@CentOS7 download
]
rm: remove symbolic
link ‘/usr/bin/chromedriver’?
yes
4.3 配置软链接
[root@CentOS7 download
]
4.4 查看版本
[root@CentOS7 download]# chromedriver --version
ChromeDriver 83.0.4103.14 (be04594a2b8411758b860104bc0a1033417178be-refs/branch-heads/4103@{#119})
5. 安装selenium
pip3
install selenium
6. 测试代码
import time
from selenium
import webdriver
def test():
chromeOptions
= webdriver
.ChromeOptions
()
chromeOptions
.add_argument
('--headless')
chromeOptions
.add_argument
('--disable-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')
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
()