如何将HTMLUnit驱动程序与Python中的Selenium一起使用? [英] How do I use the HTMLUnit driver with Selenium from Python?

查看:187
本文介绍了如何将HTMLUnit驱动程序与Python中的Selenium一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉Selenium使用HTMLUnit?

How do I tell Selenium to use HTMLUnit?

我在后台将selenium-server-standalone-2.0b1.jar作为Selenium服务器运行,并使用"pip install -U selenium"安装了最新的Python绑定.

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

一切都可以在Firefox上正常运行.但是我想使用HTMLUnit,因为它重量更轻并且不需要X.这是我的尝试:

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

问题是,HTMLUnit驱动程序叫什么?如何启用它?

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

HTMLUnit的代码似乎在Selenium 2的源代码中,因此我希望它像其他浏览器一样默认可用.我找不到有关如何启用它的说明.

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.

推荐答案

使用selenium 2.20.0.jar服务器并匹配python版本,我可以通过将浏览器指定为* mock

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

这篇关于如何将HTMLUnit驱动程序与Python中的Selenium一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆