简体   繁体   English

如何在Python中使用带有Selenium的HTMLUnit驱动程序?

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

How do I tell Selenium to use HTMLUnit? 如何告诉Selenium使用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". 我在后台运行selenium-server-standalone-2.0b1.jar作为Selenium服务器,并使用“pip install -U selenium”安装最新的Python绑定。

Everything works fine with Firefox. 一切都适用于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: 但我想使用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

So the question is, what is the HTMLUnit driver called? 所以问题是,什么是HTMLUnit驱动程序? 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. HTMLUnit的代码似乎是Selenium 2的源代码,所以我希望它像其他浏览器一样默认可用。 I can't find any instructions on how to enable it. 我找不到有关如何启用它的任何说明。

As of the 2.0b3 release of the python client you can create an HTMLUnit webdriver via a remote connection like so: 从python客户端的2.0b3版本开始,您可以通过远程连接创建HTMLUnit webdriver,如下所示:

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. 您还可以将HTMLUNITWITHJS功能项用于具有Javascript支持的浏览器。

Note that you need to run the Selenium Java server for this to work, since HTMLUnit is implemented on the Java side. 请注意,您需要运行Selenium Java服务器才能工作,因为HTMLUnit是在Java端实现的。

using the selenium 2.20.0.jar server and matching python version, I am able to use HtmlUnitDriver by specifying the browser as *mock 使用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')

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 Python 绑定将 HtmlUnit 驱动程序与 Selenium 一起使用? - How do I use the HtmlUnit driver with Selenium through the Python bindings? 我无法在Selenium和Python上使用HTMLUnit驱动程序 - I'm unable to use HTMLUnit driver with Selenium and Python 是否可以将 Python 中的 HTMLUnit 驱动程序与 Selenium 一起使用? - Is it possible to use the HTMLUnit driver in Python with Selenium? 如何在python中使用硒设置HTMLUnit驱动程序? - How to set up HTMLUnit Driver with selenium in python? 如何使用 Python 绑定在 Selenium webdriver 中加载 HtmlUnit 驱动程序? - How to load HtmlUnit driver in Selenium webdriver using Python bindings? 如何在 selenium python 的另一个测试用例中使用驱动程序对象? - How do i use driver object in another test case in selenium python? 如何使用 Python 将选项传递给 Selenium Chrome 驱动程序? - How do I pass options to the Selenium Chrome driver using Python? Python / Selenium:HTMLUNIT驱动程序连接被拒绝-端口被阻止 - Python/Selenium: HTMLUNIT Driver Connection Refused - Port Blocked Selenium Web驱动程序:如何从元素获取URL? - Selenium Web Driver: How do I get a url from an element? 如何阻止 Selenium 在执行期间关闭驱动程序? - How do I stop Selenium from closing the driver during execution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM