简体   繁体   English

Selenium 没有关闭 docker 中的浏览器进程

[英]Selenium not closing browser processes inside docker

I have a docker container:我有一个码头集装箱:

    ...
    web:
     build:
      context: .
      dockerfile: Dockerfile
     command: python /code/manage.py migrate --noinput
     command: python /code/manage.py runserver 0.0.0.0:8000
     volumes:
      - .:/code
     ports:
      - 80:8000
     environment:
      - SECRET_KEY=changemeinprod
     depends_on:
      - db
      - redis
     ...

Selenium scrape runs in this container (django project in docker): Selenium scrape 在这个容器中运行(docker 中的 django 项目):

     ...
     chrome_options = Options()
     chrome_options.add_argument("--headless")
     chrome_options.add_argument('--no-sandbox')
     chrome_options.add_argument('--disable-dev-shm-usage')
     prefs = {"profile.managed_default_content_settings.images": 2}
     chrome_options.add_experimental_option("prefs", prefs)

     driver = webdriver.Chrome(executable_path=chrome_driver_path, 
     options=chrome_options)
     driver.get(link)
     ...
     ...
     pid = driver.service.process.pid

     driver.quit()
     try:
         os.kill(int(pid), signal.SIGTERM)
         print("Killed chrome using process")
     except ProcessLookupError as e:
         pass

Despite I close driver with command driver.quit() , chrome process remains running.尽管我使用命令driver.quit()关闭driver ,chrome 进程仍在运行。 Every executing of this script keeps chrome process and makes next execution slower, because of multiple chrome processes running.由于多个 chrome 进程正在运行,此脚本的每次执行都会保持 chrome 进程并使下一次执行速度变慢。 How can I achieve closing chrome inside docker?如何在 docker 中实现关闭镀铬? Even If I need to restart container where django project running, can you tell me how to do it in python script (docker container restarts itself with python script).即使我需要重新启动运行 django 项目的容器,您能否告诉我如何在 python 脚本中执行此操作(docker 容器使用 python 脚本重新启动)。

I suggest you use Zalenium. 我建议您使用Zalenium。 It is a Selenium Grid wrapper which facilitates auto scaling/descaling. 它是一个Selenium Grid包装器,有助于自动缩放/缩放。 It will make your life much easier. 这将使您的生活更加轻松。

https://opensource.zalando.com/zalenium/ https://opensource.zalando.com/zalenium/

Try using driver.close() instead of driver.quit() .尝试使用driver.close()而不是driver.quit() Also, try adding timeout to your call out... It may just be the link taking too long to download.另外,尝试在您的呼叫中添加超时...这可能只是链接下载时间过长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM