Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the HtmlUnit driver with Selenium through the Python bindings?

I'm using WebDriver through the Python bindings located on Google's site. According to the documentation here, it supports four browsers: Chrome, IE, Firefox, and HtmlUnit. I can import the Firefox driver using from selenium.firefox.webdriver import WebDriver, and the Chrome driver using from selenium.chrome.webdriver import WebDriver.

There isn't a comparable HtmlUnit module. How do I import the HtmlUnit driver?

like image 800
Chris B. Avatar asked Nov 02 '10 20:11

Chris B.


People also ask

Does Selenium use HtmlUnit?

HtmlUnitDriver is headless driver providing non-GUI implementation of Selenium WebDriver. It is based on HtmlUnit, fastest and light-weight browser implemented in Java.

How do I use PhantomJS in Python?

To use the PhantomJS webdriver, all you need to do is change it to PhantomJS(). Then will This will work with both Python 2.7 and Python 3. On Windows, the path should be changed to the location of your phantomjs installation.


2 Answers

I found the answer at https://stackoverflow.com/a/5518175/125170

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 171
Keith Avatar answered Sep 18 '22 15:09

Keith


HtmlUnit is a Java library so the only choice for non-java WebDriver bindings is to use a RemoteWebDriver. You will need to start a Selenium Server and connect to it specifying the HtmlUnit as desired browser.

I am not very familiar with Python, but according to http://code.google.com/p/selenium/wiki/PythonBindings it should look something like:

from selenium.remote import connect
from selenium import HTMLUNIT


wd = connect(HTMLUNIT, server="http://<selenium_server>:4444")
like image 43
Sergii Pozharov Avatar answered Sep 18 '22 15:09

Sergii Pozharov