【已解决】Ubuntu19.04部署Python selenium+Chrome时遇到的各种坑

一、安装selenium

pip3 install selenium

二、安装Chrome

  • 1、下载Chrome包,注意Ubuntu系统上要下载deb格式的文件,如果直接安装,会下载最新版的chrome,而我们在使用selenium的时候,需要chrome的驱动,最新版驱动比较难找到。
  • 2、chrome各历史版本下载地址汇总:https://dl.lancdn.com/landian/soft/chrome/m/?utm_sources=DownPageBot
  • 3、我这边选择下载 75.0.3770.90_amd64.deb(后面chromedriver驱动要对应版本,实在难找刚好对应的版本啊!!)
  • 4、将下载好的文件上传到服务器任意路径上,我这边是/www/wwwroot,输入以下命令进行安装
sudo su  #以root权限登录,不然后面一直要加sudo
cd /www/wwwroot 
dpkg -i 75.0.3770.90_amd64.deb  

此时,一般会报错,网上很多方法都试了,说apt-get -f install一下就行,发现不成功
【解决】
(1)解决apt源问题,修改服务器 /etc/apt/sources.list,当然你先备份一份,修改内容如下:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
#测试版源
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
#源码
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
##测试版源
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
#Canonical 合作伙伴和附加
deb http://archive.canonical.com/ubuntu/ xenial partner
deb http://extras.ubuntu.com/ubuntu/ xenial main

(2)更新源

apt update
apt upgrade
apt-get -f install

(3)修复安装时broken的依赖

apt --fix-broken install

(4)检查apt,发现还是出现依赖错误

apt-get check 

The following packages have unmet dependencies:
libcairo2 : Depends: libpng12-0 (>= 1.2.13-4) but it is not installed
libgdk-pixbuf2.0-0 : Depends: libpng12-0 (>= 1.2.13-4) but it is not installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
Ubuntu19.04中,可以通过PPA安装libpng,安装PPA及libpng12-0的命令如下,输入以下命令:

add-apt-repository ppa:linuxuprising/libpng12
apt update
apt install libpng12-0

(5)再检查apt,无相关依赖错误的话,重新安装Chrome,成功安装

apt-get check 
dpkg -i 75.0.3770.90_amd64.deb
  • 5、查看chrome版本,下载相应版本的chromedriver:https://npm.taobao.org/mirrors/chromedriver/,我们Ubuntu的话,下载Linux版本就行了。
google-chrome --version
返回信息:Google Chrome 75.0.3770.90 表示成功

ps:我选择安装75.0.3770.90的版本,是因为驱动刚好有,若你找不到对应的,可以执行apt-get autoremove google-chrome-stable卸载,回到之前步骤重新安装

三、部署代码,尝试运行selenium

  • 1、遇到 selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path错误:
    原因: chromedriver路径设置的不对
    解决: 将上面下载的chromedriver放对位置,相对定位、绝对定位都行,我放在了项目的上一层目录,这样git下来,与本地的chromedriver不会冲突
from selenium import webdriver
	
path = "../chromedriver"
webdriver.Chrome(executable_path=path)

  • 2、遇到 selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
    (Driver info: chromedriver=76.0.3809.12 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.4.27-boot2docker x86_64)错误:

    原因: chromedriver的版本和chrom浏览器的版本不对应
    解决: 回到二,重新安装相对应的版本

  • 3、遇到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.) 错误:

    解决代码: :

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

def get_chrome_driver():
    path = "../chromedriver"  # 注意这个路径需要时可执行路径(chmod 777 dir or 755 dir)
    chrome_options = Options()
    chrome_options.add_argument('window-size=1920x3000')  # 指定浏览器分辨率
    chrome_options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--hide-scrollbars')  # 隐藏滚动条, 应对一些特殊页面
    chrome_options.add_argument('blink-settings=imagesEnabled=false')  # 不加载图片, 提升速度
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--headless')

    return webdriver.Chrome(executable_path=path, options=chrome_options)

driver = get_chrome_driver()

四、至此,selenium终于跑起来了,搞了6个小时,入了太多的坑,,最后发现好像也没有这么难,一入坑,深似海…

你可能感兴趣的:(Linux,Python,Flask)