【问题标题】:How to stop selemium chromdriver from python?如何从 python 中停止 selenium chromedriver?
【发布时间】:2020-12-20 17:51:22
【问题描述】:

我正在使用堆栈:

  • 蟒蛇
  • 码头工人

用于网页抓取。

Dockerfile 看起来像:

FROM python:3.7

# download chromedriver
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -  && \
    sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \
    apt-get -y update && \
    apt-get install -y google-chrome-stable && \
    # unzip chromedriver
    apt-get install -yqq unzip && \
    wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip && \
    unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/


WORKDIR /src

COPY requirements.txt /src/requirements.txt
RUN pip install -r /src/requirements.txt

COPY src /src

ENTRYPOINT ["python", "main.py"]

在 docker 容器中,我的代码如下所示:

from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

    DRIVER_PATH = "/usr/local/bin/chromedriver"
    URL = "google.com"
    SLEEP_TIME = 60
    options = Options()
    options.add_argument("--no-sandbox")
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")

    while True:
        browser = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
        browser.get(URL)
        browser.quit()
        sleep(SLEEP_TIME)

但是 chromdriver 开始创建僵尸进程:

所有这些进程都是由 chrome 驱动程序从主进程创建的(我现在不知道什么是“猫”):

有没有办法正确终止 webdriver?

附:我知道我只能使用一个 webdriver 实例,但是关于关闭 webdriver 的问题仍然存在。

【问题讨论】:

    标签: python docker selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    我不知道我是否充分理解了您的问题,但要终止网络驱动程序,您可以这样做:

    driver.close() 
    

    当你想要在代码中时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2018-06-15
      • 2017-02-09
      • 2023-03-22
      • 2017-05-25
      相关资源
      最近更新 更多