Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the HTMLUnit driver with Selenium from Python?

How do I tell Selenium to use HTMLUnit?

I'm running selenium-server-standalone-2.0b1.jar as a Selenium server in the background, and the latest Python bindings installed with "pip install -U selenium".

Everything works fine with Firefox. But I'd like to use HTMLUnit, as it is lighter weight and doesn't need X. This is my attempt to do so:

>>> import selenium
>>> s = selenium.selenium("localhost", 4444, "*htmlunit", "http://localhost/")
>>> s.start()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/selenium/selenium/selenium.py", line 189, in start
    result = self.get_string("getNewBrowserSession", start_args)
  File "/usr/local/lib/python2.6/dist-packages/selenium/selenium/selenium.py", line 223, in get_string
    result = self.do_command(verb, args)
  File "/usr/local/lib/python2.6/dist-packages/selenium/selenium/selenium.py", line 217, in do_command
    raise Exception, data
Exception: Failed to start new browser session: Browser not supported: *htmlunit

Supported browsers include:
  *firefox
  *mock
  *firefoxproxy
  *pifirefox
  *chrome
  *iexploreproxy
  *iexplore
  *firefox3
  *safariproxy
  *googlechrome
  *konqueror
  *firefox2
  *safari
  *piiexplore
  *firefoxchrome
  *opera
  *iehta
  *custom

So the question is, what is the HTMLUnit driver called? How do I enable it?

The code for HTMLUnit seems to be in the source for Selenium 2, so I expected it to be available by default like the other browsers. I can't find any instructions on how to enable it.

like image 759
frabcus Avatar asked Jan 06 '11 18:01

frabcus


People also ask

Does Selenium use HtmlUnit?

It is based on HtmlUnit. It is known as Headless Browser Driver. It is same as Chrome, IE, or FireFox driver, but it does not have GUI so one cannot see the test execution on screen.

How do I import a Python package into Selenium?

To install the Selenium bindings in our system, run the command: pip install selenium. As this is done, a folder called Selenium should get created within the Python folder. To update the existing version of Selenium, run the command: pip install –U selenium.

Can you use Python with Selenium Webdriver?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc.


1 Answers

As of the 2.0b3 release of the python client you can create an HTMLUnit webdriver via a remote connection like so:

from selenium import webdriver
driver = webdriver.Remote(
  desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get('http://www.google.com')

You can also use the HTMLUNITWITHJS capability item for a browser with Javascript support.

Note that you need to run the Selenium Java server for this to work, since HTMLUnit is implemented on the Java side.

like image 132
James Murty Avatar answered Oct 06 '22 00:10

James Murty