9

I've been using urllib2 to access webpages, but it doesn't support javascript, so I took a look at Selenium, but I'm quite confused even having read its docs.

I downloaded Selenium IDE add-on for firefox and I tried some simple things.

from selenium import selenium
import unittest, time, re

class test(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://www.wikipedia.org/")
        self.selenium.start()

    def test_test(self):
        sel = self.selenium
        sel.open("/")
        sel.type("searchInput", "pacific ocean")
        sel.click("go")
        sel.wait_for_page_to_load("30000")

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

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

I just access wikipedia.org and type pacific ocean in the search field, but when I try to compile it, it gives me a lot of errors.

7
  • I ran your script with no problems. Did you start the selenium server before running the script? Commented Mar 4, 2011 at 20:42
  • 3
    @sophiaw: In the download for SeleniumRC (seleniumhq.org/download) you should find a file called selenium-server.jar (e.g. SeleniumRC/selenium-server-1.0.3/selenium-server.jar). You need to have it running before you run your script. (java -jar ~/bin/selenium-server.jar 2>/dev/null 1>&2 &) Commented Mar 4, 2011 at 22:06
  • 1
    @sophiaw: See also seleniumhq.org/docs/05_selenium_rc.html#installation Commented Mar 4, 2011 at 22:07
  • 1
    @sophiaw: could you edit the question anyway with some details about the errors you were getting, and maybe summarize what it took to fix it as an answer? Commented May 6, 2011 at 10:35
  • 1
    @unutbu Could you post your suggestion as an answer. It seems to have helped the OP's problem. Commented May 11, 2011 at 20:14

3 Answers 3

6

If running the script results in a [Errno 111] Connection refused error such as this:

% test.py
E
======================================================================
ERROR: test_test (__main__.test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/unutbu/pybin/test.py", line 11, in setUp
    self.selenium.start()
  File "/data1/unutbu/pybin/selenium.py", line 189, in start
    result = self.get_string("getNewBrowserSession", [self.browserStartCommand, self.browserURL, self.extensionJs])
  File "/data1/unutbu/pybin/selenium.py", line 219, in get_string
    result = self.do_command(verb, args)
  File "/data1/unutbu/pybin/selenium.py", line 207, in do_command
    conn.request("POST", "/selenium-server/driver/", body, headers)
  File "/usr/lib/python2.6/httplib.py", line 898, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 935, in _send_request
    self.endheaders()
  File "/usr/lib/python2.6/httplib.py", line 892, in endheaders
    self._send_output()
  File "/usr/lib/python2.6/httplib.py", line 764, in _send_output
    self.send(msg)
  File "/usr/lib/python2.6/httplib.py", line 723, in send
    self.connect()
  File "/usr/lib/python2.6/httplib.py", line 704, in connect
    self.timeout)
  File "/usr/lib/python2.6/socket.py", line 514, in create_connection
    raise error, msg
error: [Errno 111] Connection refused

----------------------------------------------------------------------
Ran 1 test in 0.063s

FAILED (errors=1)

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 &

You will find more complete instructions on how to set up the server here.

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

Comments

1

I would suggest you to use a webdriver, you can find it here: http://code.google.com/p/selenium/downloads/list. If you want to write tests as a coder (and not with the use of your mouse), that thing would work better then the RC version you're trying to use, at least because it would not ask you for an SeleniumRC Jar Instance. You would simply have a binary of a browser or use those ones that are already installed on your system, for example, Firefox.

Comments

-1

I faced with this issue in my project and found that problem was in few webdriver.get calls with very small time interval between them. My fix was not to put delay, just remove unneeded calls and error disappears. Hope, it can helps for somebody.

Comments

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.