调用 snapshot_selenium 报错 The process started from chrome location /usr/bin/google-chrome is no longer

        今天从windows环境运行了一段pyecharts的示例代码没有啥问题.

        结果迁移到了 Ubuntu 18.04环境后竟然无法运行.代码及错误信息如下:

from snapshot_selenium import snapshot as driver

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot


def bar_chart() -> Bar:
    c = (
        Bar()
        .add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
        .add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
        .add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
        .set_global_opts(title_opts=opts.TitleOpts(title="Bar-测试渲染图片"))
    )
    return c

# 需要安装 snapshot-selenium 或者 snapshot-phantomjs
make_snapshot(driver, bar_chart().render(), "bar.png")

错误信息:

Traceback (most recent call last):
  File "/home/mg/github/RefUtils/src/fh_tools/language_test/pyecharts/simple_test.py", line 30, in 
    make_snapshot(driver, bar_chart().render(), "bar.png")
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/pyecharts/render/snapshot.py", line 37, in make_snapshot
    **kwargs,
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/snapshot_selenium/snapshot.py", line 35, in make_snapshot
    driver = get_chrome_driver()
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/snapshot_selenium/snapshot.py", line 58, in get_chrome_driver
    return webdriver.Chrome(options=options)
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/mg/github/RefUtils/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
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.)

查了一些参考信息

从Chrome位置/ usr / bin / google-chrome开始的流程已不再运行,因此ChromeDriver假设Chrome已针对Selenium崩溃

但由于 snapshot_selenium 本身已经通过 get_chrome_driver 函数将 driver 创建的代码封装起来了,所以,只好直接该他的源码了.

操作方法如下:

1) 顺着代码往源头点开,直到 

venv/lib/python3.6/site-packages/snapshot_selenium/snapshot.py

 

2)看到第55行的代码 get_chrome_driver 如下

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

修改为:

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)

再次运行,完美通过.

虽然修改已经安装包的源码不是一个好的解决方案,不过临时处理一些,还是蛮快的.

后续改成mock的形式来解决这个问题,即可


第二天,有了更好的解决方案

https://blog.csdn.net/mmmaaaggg/article/details/105305669

 

你可能感兴趣的:(webdriver,ubuntu,python,python,selenium,linux)