linux上python selenium爬虫和pyrcharts make_snapshot截图时报错:DevToolsActivePort file doesn‘t exist

linux在单独使用pyecharts画图用snapshot-selenium 或者 snapshot-phantomjs中的make_snapshot截图保存的时候没有啥问题出现,

使用了selenium爬虫去截图的,用的是谷歌浏览器插件,单独运行没有问题

driver.find_element_by_xpath('/html/body/div[5]/section/div[10]/div[3]/table').screenshot("table.png")

设置好无界面参数和不需要root权限的参数就可以正常爬虫:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')

 但是呢,两个放在同一个进程当中,问题就出现了,先运行了爬虫函数后,再去调用make_snapshot截pyecharts画的图就会出现以下报错:

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

后来发现其实make_snapshot截图的时候就是本身已经通过get_chrome_driver函数将 driver创建的代码封装起来了,所以,只好直接改他的源码了.。但是为啥单独运行的时候没问题一起运行就有bug我也没想明白,因机器而异。

所以只能通过改make_snapshot的源码,设置一下get_chrome_driver时候的参数就行了

 

操作方法如下:

1) 顺着代码往源头点开,或者在报错信息中直接点开报错的位置可以找到

我安装的位置是在: /opt/python3.6/lib/python3.6/site-packages/snapshot_selenium/snapshot.py

def get_chrome_driver():
    options = webdriver.ChromeOptions()
    options.add_argument("headless")
    return webdriver.Chrome(options=options)

将上面加的参数添加到这里面就ok了

def get_chrome_driver():
    options = webdriver.ChromeOptions()
    options.add_argument("headless")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-dev-shm-usage')
    return webdriver.Chrome(options=options)

2)另一种方式就是改成mock的形式:

from unittest import mock
 
 
def get_chrome_driver():
    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument("headless")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-dev-shm-usage')
    return webdriver.Chrome(options=options)

使用make_snapshot的时候重新获取谷歌插件就ok了~

 

with mock.patch('snapshot_selenium.snapshot.get_chrome_driver', get_chrome_driver):
    # 需要安装 snapshot-selenium 或者 snapshot-phantomjs
    make_snapshot(driver, bar_chart().render(), "bar.png")

重点是最顶部以及最底部的几行代码.

1)重新定义 get_chrome_driver函数

2)通过mock方式替换 snapshot_selenium.snapshot.get_chrome_driver 函数.关键语句:

with mock.patch('snapshot_selenium.snapshot.get_chrome_driver', get_chrome_driver)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值