Python selenium webdriver - the driver suddenly "dies" and cannot leave, get current_url, open pages

Sometimes, in the middle of my script, my webdriver instance just dies!

And from now on, I can not refer to any of his methods.

Some examples:

>>> spsel.driver.current_url Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 414, in current_url return self.execute(Command.GET_CURRENT_URL)['value'] File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in execute response = self.command_executor.execute(driver_command, params) File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 280, in execute return self._request(url, method=command_info[0], data=data) File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 321, in _request response = opener.open(request) File "/usr/lib/python2.6/urllib2.py", line 391, in open response = self._open(req, data) File "/usr/lib/python2.6/urllib2.py", line 409, in _open '_open', req) File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open raise URLError(err) urllib2.URLError: <urlopen error [Errno 111] Connection refused> >>> spsel.driver.quit() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 55, in quit RemoteWebDriver.quit(self) File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 443, in quit self.execute(Command.QUIT) File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in execute response = self.command_executor.execute(driver_command, params) File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 280, in execute return self._request(url, method=command_info[0], data=data) File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 321, in _request response = opener.open(request) File "/usr/lib/python2.6/urllib2.py", line 391, in open response = self._open(req, data) File "/usr/lib/python2.6/urllib2.py", line 409, in _open '_open', req) File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open raise URLError(err) urllib2.URLError: <urlopen error [Errno 111] Connection refused> 

Any ideas why this could be so? Any better solutions to overcome?

I am thinking about occasionally testing liveness on driver.current_url in a try block, and if it throws an exception, then installing the driver on None and then re-creating the instance ... but it's an ugly hack, and I don’t understand why it is necessary.

+7
source share
4 answers

After an endless struggle with selenium drivers and the FF extension. I completely deleted it.

I am using http://www.phantomjs.org/ , which is a headless JS lib. It works like a charm. (and I want to see the page, you can always show the screen)

I mainly work in ruby: replace capybara-webkit with a poltergeist (which is just js_driver for capybara)

I am sure there will be a similar solution. Maybe this will not answer your question, but it will give a great look at js testing.

+1
source

Do you have a selenium web server running first?

This question assumes the web server is down - issue: Selenium in Python

  then the solution is most likely that you need get the selenium server running first.

 In the download for SeleniumRC you will find a file called selenium-server.jar (as of a 
 few months ago, that file was located at SeleniumRC / selenium-server-1.0.3 / selenium-
 server.jar).

 On Linux, you could run the selenium server in the background with the command

 java -jar /path/to/selenium-server.jar 2> / dev / null 1> & 2 &

More detailed server configuration instructions can be found here http://seleniumhq.org/docs/05_selenium_rc.html#installation

This page has a similar problem: http://johnmudd.infogami.com/blog/5be6

Another similar question: How do you connect remotely using Python + Webdriver

0
source

So, this is a "refused connection" error with your selenium web driver in Firefox, which is implemented as a Firefox extension. The wild guess here is a problem in the extension, or Firefox stops working with the httpd code in the extension.

You can try to check if you have the latest web driver extension and compatible version of Firefox.

And also you can try another alternative browser, for example. Chrome Web Driver (requires a few lines of change to your Python code)

0
source

I had the same problem, or at least I think so. On my side, the browser (Chrome) will be blocked, and when the process in the terminal manually stops, I get the same URLError.

While I used Django LiveServerTestCase as well as Splinter and used the following methods:

 @classmethod def setUpClass(cls): cls.browser = Browser() super(MyClass, cls).setUpClass() @classmethod def tearDownClass(cls): cls.browser.quit() super(MyClass, cls).tearDownClass() 

This will create only one driver for all tests performed by the class. I also put all my tests with only one method.

In any case, this will block the driver at some point.

Then I switched to implementing the Setup method and TearDown in the driver instance / exit in them. Then it would create and leave a driver for each test (metho) in the class. I also broke my tests into several methods.

After that, everything will work fine, while I do the same. So the problem seems to be basically doing everything with one driver.

As a hint, you can also introduce input elements into the installation method so that your driver is registered for each test, this is necessary, since turning off the driver also disconnects the session.

Here is the result:

 from splinter import Browser from django.test import LiveServerTestCase from django.core.urlresolvers import reverse class MySeleniumTests(LiveServerTestCase): fixtures = ['initial_data.json'] def setUp(self): #fire up your driver self.browser = Browser('chrome') #login self.browser.visit('%s%s' % (self.live_server_url, reverse('home'))) self.assertEquals(self.browser.url, '%s%s' % (self.live_server_url,'/accounts/login/?next=%s' % reverse('home'))) self.browser.fill_form({'username': 'test', 'password': 'test'}) self.browser.find_by_tag('button').first.click() self.assertEquals(self.browser.url, '%s%s' % reverse('home'))) def tearDown(self): #quit your driver self.browser.quit() 
0
source

All Articles