6

New to this, apologies for the novice question.

Trying to run a script using Python, Selenium and the unittest module. Have the typical setUp(), test_1, test_2, tearDown() method structure. Since I've added in more than one test, I get the following error: selenium.common.exceptions.InvalidSessionIdException: Message: Tried to run command without establishing a connection

How can I resolve this?

I have looked into similar problems people have been facing with this issue, but in almost all cases the issue is not related to anything I am encountering (cronjobs for example)

My program looks like this...

class MyTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        #my setup code here...
        cls.driver = webdriver.Firefox(executable_path='my_gecko_driver')
        cls.driver.get('www.my_url.com')
        cls.driver......  # various other tasks

    def test_1(self):
        # my test code here....
        foo = self.driver.find_element_by_xpath('/button_elem/')
        foo.click()
        # etc etc....

    def test_2(self):
        # my test code here....
        bar = self.driver.find_element_by_xpath('/button_elem/')
        bar.click()
        # etc etc....

    @classmethod
    def tearDown(cls):
        print('Entered tearDown function.')
        # close the browser window
        cls.driver.close()


if __name__ == '__main__':
unittest.main()

Before I added the second test, the test ran successfully.

Now I am getting the error: selenium.common.exceptions.InvalidSessionIdException: Message: Tried to run command without establishing a connection

I suspect this is to do with the tearDown method perhaps not working correctly? However I thought this method was called at the end of every test_x upon finishing.

I have also noticed that Pycharm is highlighting 'driver' in the line 'cls.driver.close()' which I am also not too sure about. It says unresolved attribute reference' however is this not created in the setUp() method?

NikoCon
  • 99
  • 1
  • 2
  • 10
  • driver.close closes the browser window... (and for some drivers will also quit the driver if that's the last window open) After that you won't be able to do anything because there is no window or document to check for elements. – pcalkins Jul 16 '19 at 22:29
  • Thanks for the reply @pcalkins, what is my alternative then? I have tried driver.close() and driver.quit() both lead me to the same error. – NikoCon Jul 17 '19 at 07:59
  • either don't close it (you should probably use .quit() if you do want to close the driver... that will close the browser too) or create a new driver instance and open a URL before finding an element. – pcalkins Jul 17 '19 at 16:55

2 Answers2

0

Try switching explicitly between tabs before closing them.

main_page = driver.window_handles[0]  
driver.switch_to.window(main_page)
0

this is because multiple browser sessions are opened at your machine. if you are on linux run the command killall firefox

and try to run your script again. This should fix error for you.