First, I'm using Django, and I'm building an API. For example, I've route like api/get-some-element
.
browser_url = cache.get('user_%d.browser_url' % user_id)
is_browser_queued = cache.get('user_%d.browser_queued' % user_id)
browser_session_id = cache.get('user_%d.browser_session_id' % user_id) # session id of the main chrome
if browser_url:
capabilities = DesiredCapabilities.CHROME.copy()
driver = webdriver.Remote(
command_executor=browser_url,
desired_capabilities=capabilities,
options=options,
)
driver.session_id = browser_session_id
return driver
if not is_browser_queued:
run_browser.delay(user_id) # launch main chrome
cache.set('user_%d.browser_queued' % user_id, True, None)
On the first access to that route, it will send a task to Celery to launch Selenium Headless Chrome (main chrome). Why I'm using Celery? Because I need to make Chrome always running. (Idk better way, this is what I know).
Then, for the next access on that route, it will response with Waiting chrome to launched.
, until the Celery task got executed (chrome launched properly).
After main chrome is launched, then it will launch Selenium Headless Remote Driver immediately (without Celery), the purpose of this remote driver is to access the main chrome. Then, it just grab some element from a website.
But, after finish, the Remote Driver is still running. How to stop that?
I know the command such as driver.quit()
or driver.close()
. But, as far as I know, it's send the command to the main chrome, not to the chrome launched by remote driver. And that's not what I want. I don't want to quit the main chrome, just quit the chrome launched by remote driver.