0

I have two functions using selenium webdriver. One logs in, and another that performs authenticated actions on the website.

from selenium import webdriver

def login(driver):
    driver.get('website.com')
    # login

    return driver

def do_stuff(driver):
    driver.get('website.com/things')
    # do stuff

    return driver

if __name__ == '__main__':
    driver = webdriver.Firefox()

    driver = login(driver)
    driver = do_stuff(driver)

    driver.close()

The first function successfully logs in, but when the second function runs, I get the following error:

selenium.common.exceptions.InvalidSessionIdException: Message: Tried to run command without establishing a connection

I checked the session_id of the driver at the end of the first function and at the beginning of the first function, and they are the same. Also, it should not be an issue with the specific commands in the script, because it works when not split into two functions. I can provide a more specific code sample if it helps.

What am I missing, is there a way to get this to work?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Please don't change the question once you have recieved canonical answers. If you change the question, the existing answers won't be helpful to the future readers. For the time being I have reverted back your question to it's initial state. – undetected Selenium Apr 10 '22 at 23:29
  • As I know there is no need to return driver. – furas Apr 10 '22 at 23:31
  • @furas my intention is to allow running `do_stuff()` multiple times without having to re-run `login()` every time, rather pass the driver that has the login session into the function. – Zach Taliaferro Apr 10 '22 at 23:45
  • @undetectedSelenium my original question was unchanged, I only added extra details for context, as the answer that you posted below is not relevant to my issue – Zach Taliaferro Apr 10 '22 at 23:46
  • you can run many times without re-runining `login()` but you don't have to `return driver` from `login` because `login` gets reference to object `driver` and when you use `driver` inside `login` then it changes also `driver` which is outside `login()`. You didn't show FULL error message so we can't see in which line it makes problem - but it looks like it couldn't connect to page. Better show real url which you try to connect. – furas Apr 11 '22 at 00:23
  • maybe you need [webdriver_manager](https://github.com/SergeyPirogov/webdriver_manager) to automatically download and install correect driver for your browser. – furas Apr 11 '22 at 00:32
  • Thank you, good to know the driver does not need to be returned and can be continually used as you describe. After some a closer look, it seems I had an `assert` that was using `!=` instead of `==`, which was inside of a try/except block. On `except`, I was running `driver.close()`. I then was running `driver.close()` again outside of the function calls. Since the driver had already been closed, this is what tripped the error. I thought it was an issue passing the driver between the functions because the `assert` was stopping the function unexpectedly. – Zach Taliaferro Apr 11 '22 at 00:45

1 Answers1

1

I took your code and made a couple of simple tweaks to adjust with my environment, added print() statement and executed your program and seems it runs perfecto.

based code

Code Block:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

def login(driver):
    driver.get('https://www.selenium.dev/')
    print("Within login")
    return driver

def do_stuff(driver):
    driver.get('https://www.selenium.dev/documentation/')
    print("Within do_stuff")
    return driver

if __name__ == '__main__':
    driver = webdriver.Firefox(service=Service('C:\\BrowserDrivers\\geckodriver.exe'))
    driver = login(driver)
    driver = do_stuff(driver)
    driver.quit()
    

Console Output:

Within login
Within do_stuff

Update

If you are still facing the InvalidSessionIdException error you need to cross check the compatibility of the versions of the binaries you are using.

You can find a relevant detailed discussion in selenium.common.exceptions.InvalidSessionIdException using GeckoDriver Selenium Firefox in headless mode through Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • To clarify, my code works in a similar fashion as yours, I'll go ahead and edit above to provide more specific code. – Zach Taliaferro Apr 10 '22 at 23:05
  • In response to your edit, I checked my versions of Geckodriver, Selenium, and Firefox, and my Firefox version was too high according to this chart: https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html Geckodriver: 0.30 Selenium: 4.1.3 Firefox: 99 I downgraded Firefox to 75, and am still having the same issue – Zach Taliaferro Apr 10 '22 at 23:29
  • @ZachTaliaferro You need to address that issue and you will be good to go. – undetected Selenium Apr 10 '22 at 23:32
  • Sorry, I accidentally hit submit on my comment before I finished. See my previous comment. – Zach Taliaferro Apr 10 '22 at 23:42
  • If you are using Geckodriver: 0.30 and Selenium: 4.1.3, Firefox: 99 is good to go. Seems they haven't updated the document. – undetected Selenium Apr 10 '22 at 23:46