Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium in Python

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.

like image 410
sophiaw Avatar asked Mar 04 '11 20:03

sophiaw


2 Answers

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.

like image 188
unutbu Avatar answered Nov 09 '22 14:11

unutbu


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.

like image 1
Pavel Daynyak Avatar answered Nov 09 '22 14:11

Pavel Daynyak