How to terminate session in Selenium Grid(Extras)

Viewed 23652

How to terminate session in Selenium Grid? My problem is that if my test fail hub still keep session for this test and I can't run another test (it run but fail because can't get free node, because it is registered in hub). I have found this How to kill thread in a Selenium Grid node and there is an answer to use DELETE /session/:sessionId but it didn't work for me. Documentation on Selenium Grid or Extras is very pure, maybe some one have similar issue and know how to resole it?

2 Answers

The delete method works for me. This is my python code:

def clear_sessions(session_id=None):
    """
    Here we query and delete orphan sessions
    docs: https://www.selenium.dev/documentation/grid/advanced_features/endpoints/
    :return: None
    """
    url = "http://127.0.0.1:4444"
    if not session_id:
        # delete all sessions
        r = requests.get("{}/status".format(url))
        data = json.loads(r.text)
        for node in data['value']['nodes']:
            for slot in node['slots']:
                if slot['session']:
                    id = slot['session']['sessionId']
                    r = requests.delete("{}/session/{}".format(url, id))
    else:
        # delete session from params
        r = requests.delete("{}/session/{}".format(url, session_id))

I hope this is helpful for you

Related