【问题标题】:How do I use the HTMLUnit driver with Selenium from Python?如何将 HTMLUnit 驱动程序与 Python 中的 Selenium 一起使用?
【发布时间】:2011-01-06 18:29:37
【问题描述】:

我如何告诉 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 的源代码中,所以我希望它像其他浏览器一样默认可用。我找不到任何关于如何启用它的说明。

【问题讨论】:

标签: python selenium-rc htmlunit selenium-webdriver


【解决方案1】:

从 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 端实现的。

【讨论】:

  • 我运行 selenium-server-standalone-3.141.59,但它不起作用!
  • @ghanem 这个答案来自 2011 年,您可能需要找到更新的解决方案
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我是这样使用的:

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

    【讨论】:

    • HtmlUnit 在哪里提供 Selenium 1 接口?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多