'How to stop selemium chromdriver from python?

I am using stack:

  • selenium
  • python
  • docker

for web scraping.

Dockerfile looks like:

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"]

Inside docker container my code looks like:

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)

But chromdriver start to create zombie processes:

enter image description here

All of this process created from main process by chrome driver (I don't now what is 'cat'):

enter image description here

Is there a way to terminate the webdriver correctly?

P.S. I understand that I can use only one instance of webdriver, but the question about closing webdriver is still actual.



Solution 1:[1]

I don't know if I well understood your question but to terminate the webdriver you can just do :

driver.close() 

when you want in the code.

Solution 2:[2]

driver.close() is just closing the page open/download from Chromedriver. In fact, the Chromedriver is kept running as checked by

ps aux

Instead, driver.quit() will stop the Chromedriver running in the background.

Solution 3:[3]

# use these code it will work   and also check selenium version  use  3.14.0
#sometime selenium version can be occur
#check
#by import selenium
#print (selenium.__version__)
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.close()
    sleep(SLEEP_TIME)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tangs
Solution 2 joanis
Solution 3 Deepak Gautam