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

纵然是瞬间 提交于 2019-11-27 12:27:31

问题


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.


回答1:


using the selenium 2.20.0.jar server and matching python version, I am able to use HtmlUnitDriver by specifying the browser as *mock

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

server_url = "http://%s:%s/wd/hub" % (test_host, test_port)
dc = DesiredCapabilities.HTMLUNIT
wd = webdriver.Remote(server_url, dc)
wd.get('http://www.google.com')



回答2:


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.




回答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')))


来源:https://stackoverflow.com/questions/4618373/how-do-i-use-the-htmlunit-driver-with-selenium-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!