Home > Mobile >  InvalidSessionIdException while Selenium driver is not closed
InvalidSessionIdException while Selenium driver is not closed

Time:03-28

I have the following snippet of my code:

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 60)

for _ in range(10):
    try:
       driver.get(URL)
       wait.until(EC.presence_of_element_located((By.XPATH, ".//*[@class='sr-match-default__status-pre']")))
       driver.close()
        # do something
    except TimeoutException:
        time.sleep(300)
        continue
else:
    driver.close()

This yields a selenium.common.exceptions.InvalidSessionIdException but I don't know why.

When I look at the error, the docs say the following:

Occurs if the given session id is not in the list of active sessions, meaning the session either does not exist or that it’s not active.

This is not the case with my code. Earlier in my script / code I define and use the driver successfully before closing it. In addition, the driver is able to retrieve the URL without ay problems. Please advice

The full error log looks as follows:

  File "/scheduler.py", line 24, in get_new_matches
    driver.get(URL)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 436, in get
    self.execute(Command.GET, {'url': url})
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
Stacktrace:
0   chromedriver                        0x0000000102fe7159 chromedriver   5120345
1   chromedriver                        0x0000000102f74b13 chromedriver   4651795
2   chromedriver                        0x0000000102b64d1f chromedriver   392479
3   chromedriver                        0x0000000102b8ce94 chromedriver   556692
4   chromedriver                        0x0000000102bb7abc chromedriver   731836
5   chromedriver                        0x0000000102bb5885 chromedriver   723077
6   chromedriver                        0x0000000102bb5127 chromedriver   721191
7   chromedriver                        0x0000000102b3d7da chromedriver   231386
8   chromedriver                        0x0000000102fa438d chromedriver   4846477
9   chromedriver                        0x0000000102fbe21c chromedriver   4952604
10  chromedriver                        0x0000000102fc3a12 chromedriver   4975122
11  chromedriver                        0x0000000102fbeb4a chromedriver   4954954
12  chromedriver                        0x0000000102f995b0 chromedriver   4801968
13  chromedriver                        0x0000000102b3c8ae chromedriver   227502
14  libdyld.dylib                       0x00007fff6757dcc9 start   1
15  ???                                 0x0000000000000002 0x0   2

CodePudding user response:

You are missing a break statement in the for, without it the code behaves as the loop wasn't successful and executes the else block where you close the driver for a second time. See Why does python use 'else' after for and while loops? for more detailed explanation.

Adding break statement at the end of the try block should solve the issue.

  • Related