Home > Software design >  Can`t attach to detached selenium window in python
Can`t attach to detached selenium window in python

Time:08-24

Cant send commands to selenium webdriver in detached session because link http://localhost:port died.

But if i put breakpoint 1 link stay alive

import multiprocessing
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def create_driver_pool(q):
    options = Options()
    driver = webdriver.Chrome(options=options)
    pass #breakpoint 1
    return driver.command_executor._url

windows_pool = multiprocessing.Pool(processes=1)
result = windows_pool.map(create_driver_pool, [1])
print(result)
pass # breakpoint 2 for testing link

why is this happening and what can i do about it?

CodePudding user response:

After some research i finally found the reason for this behavor. Thanks https://bentyeh.github.io/blog/20190527_Python-multiprocessing.html and some googling about signals. This is not signals at all. I found this code in selenium.common.service

    def __del__(self):
        print("del detected")
        # `subprocess.Popen` doesn't send signal on `__del__`;
        # so we attempt to close the launched process when `__del__`
        # is triggered.
        try:
            self.stop()
        except Exception:
            pass

This is handler for garbage collector function, that killing subprocess via SIGTERM

                self.process.terminate()
                self.process.wait()
                self.process.kill()
                self.process = None

But if you in the debug mode with breakpoint, garbage collector wont collect this object, and del wont start.

  • Related