1. 安装Google
# 下载安装包
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# 更新源
$ sudo apt-get -f install
# 解压安装包
$ sudo dpkg -i google-chrome-stable_current_amd64.deb
2. 安装chromedriver
# 查看chrome位置
$ whereis google-chrome
# 下载chromedriver并上传到服务器
# 网址为:http://chromedriver.storage.googleapis.com/index.html
# 根据下载的谷歌版本去下载对应的版本并放入文件夹外
$ sudo mv {chromedriver地址} /usr/bin
3. 测试是否可以正常使用
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('--disable-gpu')
option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-shm-usage') # 克服有限的资源问题,不加会报错
driver = webdriver.Chrome(executable_path='chromedriver', chrome_options=option)
driver.get("http://www.baidu.com")
page_src = driver.page_source
print(page_src)
driver.quit()
4. 问题及解决办法
1. 权限问题
PermissionError: [Errno 13] Permission denied: ‘chromedriver’
During handling of the above exception, another exception occurred:
- 解决办法
$ whereis chromedriver # 找到chromediver
# 返回:chromedriver:
$ cd /usr/bin
$ sudo chmod +x chromedriver # 给予权限
2.参数问题
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
解决办法
# 代码里加入以下参数
options = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--no-sandbox')
option.add_argument('--disable-gpu')
option.add_argument('--disable-dev-shm-usage')