16

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?

4 Answers 4

16

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.

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

1 Comment

would help if provide url talk about : Note that you need to run the Selenium Java server for this to work, since HTMLUnit is implemented on the Java side.
5

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")

3 Comments

Except that you can use the IE, Chrome, and Firefox drivers through the Python bindings. And code.google.com/p/selenium/wiki/PythonBindings says "All of the browsers supported by the Java implementation of Selenium are available in the Python bindings".
But HtmlUnit is not a browser - it is a Java framework for unit testing of html/web apps
HTMLUnit is a browser. Just because you can't see it render doesn't make it any less of a browser–it just means it's headless.
3

I use it like this:

from selenium.remote import connect                                                                                                                          

b = connect('htmlunit')                                                                                                                                      
b.get('http://google.com')                                                                                                                                   

q = b.find_element_by_name('q')                                                                                                                              
q.send_keys('selenium')                                                                                                                                      
q.submit()                                                                                                                                                   

for l in b.find_elements_by_xpath('//h3/a'):                                                                                                                 
    print('%s\n\t%s\n' % (l.get_text(), l.get_attribute('href')))

1 Comment

no longer works: ImportError: No module named remote
0

I agree with the answer above, but the server must be opened before that. After downloading the server and the htmlunitdriver, cmd should be opened in the same file. and this should be written to cmd.

java -cp "htmlunit-driver-2.47.1-jar-with-dependencies.jar; selenium-server-standalone-3.141.0.jar" org.openqa.grid.selenium.GridLauncherV3

Of course the version may be different. Update the code accordingly. Also the code I use for htmlunitdriver in python:

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

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.