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?

7
  • 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. Commented Apr 10, 2022 at 23:29
  • As I know there is no need to return driver. Commented Apr 10, 2022 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. Commented Apr 10, 2022 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 Commented Apr 10, 2022 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. Commented Apr 11, 2022 at 0:23

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

To clarify, my code works in a similar fashion as yours, I'll go ahead and edit above to provide more specific code.
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: firefox-source-docs.mozilla.org/testing/geckodriver/… Geckodriver: 0.30 Selenium: 4.1.3 Firefox: 99 I downgraded Firefox to 75, and am still having the same issue
@ZachTaliaferro You need to address that issue and you will be good to go.
Sorry, I accidentally hit submit on my comment before I finished. See my previous comment.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.