如何在Python中使用Selenium的HTMLUnit驱动?

13 投票
3 回答
18865 浏览
提问于 2025-04-16 09:31

我该怎么告诉Selenium使用HTMLUnit呢?

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

在Firefox上一切都运行得很好。但我想使用HTMLUnit,因为它更轻便,不需要X环境。这是我尝试的代码:

>>> 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驱动的名字是什么?我该怎么启用它呢?

HTMLUnit的代码似乎在Selenium 2的源代码里,所以我以为它和其他浏览器一样,默认就可以使用。我找不到任何关于如何启用它的说明。

3 个回答

2

我这样使用它:

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

在python客户端的2.0b3版本中,你可以通过远程连接来创建一个HTMLUnit的webdriver,方法如下:

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

你还可以使用HTMLUNITWITHJS这个选项来创建一个支持Javascript的浏览器。

需要注意的是,要让这个功能正常工作,你需要运行Selenium的Java服务器,因为HTMLUnit是在Java这边实现的。

5

我使用的是selenium 2.20.0.jar这个服务器,并且配合相应的Python版本,这样我就可以通过指定浏览器为*mock来使用HtmlUnitDriver。

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

撰写回答