Ubuntu18.0使用Selenium

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')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值