首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将HTMLUnit驱动程序与来自Python的Selenium一起使用?

如何将HTMLUnit驱动程序与来自Python的Selenium一起使用?
EN

Stack Overflow用户
提问于 2011-01-07 02:29:38
回答 3查看 17.2K关注 0票数 13

如何告诉Selenium使用HTMLUnit?

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

使用Firefox时,一切都很正常。但我想使用HTMLUnit,因为它更轻,不需要X。这是我尝试这样做的尝试:

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-09 06:05:45

使用selenium 2.20.0.jar服务器和匹配的python版本,我可以通过将浏览器指定为* HtmlUnitDriver来使用它

代码语言:javascript
运行
复制
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')
票数 5
EN

Stack Overflow用户

发布于 2011-04-02 03:55:23

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

代码语言:javascript
运行
复制
from selenium import webdriver
driver = webdriver.Remote(
  desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get('http://www.google.com')

您还可以将HTMLUNITWITHJS功能项用于支持Javascript的浏览器。

请注意,您需要运行Selenium Java服务器才能正常工作,因为HTMLUnit是在Java端实现的。

票数 15
EN

Stack Overflow用户

发布于 2011-02-17 20:42:27

我是这样使用它的:

代码语言:javascript
运行
复制
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')))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4618373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档