python-3.x 获取错误消息“Service chromedriver unexpected exit.状态代码为:-15”

9avjhtql  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(111)

我遇到了以下问题:
我在Airflow中执行DAG,根据一系列城市收集天气信息。当DAG执行超过1个城市时,一切正常。但是,当我只需要从一个城市刮擦时,我从selenium得到一个错误,消息Service chromedriver意外退出。状态代码为:-15。我到处找了,但找不到这段代码代表什么,并修改了代码以解决任何逻辑问题,但一切都很好。下面是我启动驱动程序的函数:

def start_driver():
    localdir = os.path.dirname(os.path.realpath(__file__))
    options = webdriver.ChromeOptions()
    prefs = {'download.default_directory': localdir}
    os.system('sudo chmod 777 {}'.format(localdir))
    options.add_experimental_option('prefs', {
        "plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}],
        "download": {
        "prompt_for_download": False,
        "default_directory"  : localdir
        }
    })
    options.add_argument('--headless')
    options.add_argument('--ignore-certificate-errors')

    # add acceptInsecureCerts
    capabilities = options.to_capabilities()
    capabilities['acceptInsecureCerts'] = True

    root_path = '/home/airflow/airflow/dags'

    exe_path = os.path.join(root_path, 'chromedriver')

    driver = webdriver.Chrome(exe_path, chrome_options=options, desired_capabilities=capabilities)

    return driver

字符串
下面是完整的错误信息:

Traceback (most recent call last):
  File "/home/airflow/airflow/dags/scripts/extract.py", line 625, in execute_extraction_task
    start_crawler(city_list, cursor)
  File "/home/airflow/airflow/dags/scripts/extract.py", line 231, in start_crawler
    driver = start_driver()
  File "/home/airflow/airflow/dags/scripts/extract.py", line 155, in start_driver
    driver = webdriver.Chrome(exe_path, chrome_options=options, desired_capabilities=capabilities)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -15


感谢您的帮助!

4szc88ey

4szc88ey1#

首先,这里有几个选项可以帮助:

options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")

字符串
接下来,您使用的是旧版本的selenium,它不包含重要的更新。尽管您没有直接指定您使用的是哪个版本,但我可以看到您仍然通过webdriver.Chrome()传递executable_path,但在最新版本中已将其移动到Service()selenium在最新版本中还包括一个驱动程序管理器,以确保您拥有正确的驱动程序。下面是一个代码示例:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service()
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()


如果在您的系统PATH中没有找到正确的驱动程序,Selenium Manager将自动下载它。
以下是如何在新的selenium4.10.0中使用service参数指定executable_path

service = Service(executable_path='./chromedriver')


(在最新的selenium版本中,指定executable_path是可选的。)

相关问题