'Message: Tried to run command without establishing a connection
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?
Solution 1:[1]
Try switching explicitly between tabs before closing them.
main_page = driver.window_handles[0]
driver.switch_to.window(main_page)
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Кирилл Бондарь |
Solution 2 | yogesh deshpande |