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?
do_stuff()multiple times without having to re-runlogin()every time, rather pass the driver that has the login session into the function.login()but you don't have toreturn driverfromloginbecauselogingets reference to objectdriverand when you usedriverinsideloginthen it changes alsodriverwhich is outsidelogin(). 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.