2010-11-02 99 views

回答

5

HtmlUnit是一个Java库,所以非java WebDriver绑定的唯一选择是使用RemoteWebDriver。您需要启动Selenium服务器并连接到它,并将HtmlUnit指定为所需的浏览器。

我不是很熟悉Python,但根据http://code.google.com/p/selenium/wiki/PythonBindings它应该是这个样子:

from selenium.remote import connect 
from selenium import HTMLUNIT 


wd = connect(HTMLUNIT, server="http://<selenium_server>:4444") 
+1

但您可以通过Python使用IE,Chrome和Firefox的驱动程序绑定。而http://code.google.com/p/selenium/wiki/PythonBindings说“Selenium的Java实现支持的所有浏览器都可以在Python绑定中使用”。 – 2010-11-02 21:01:05

+3

但HtmlUnit不是浏览器 - 它是用于HTML/Web应用程序单元测试的Java框架 – 2010-11-03 10:29:05

+0

HTMLUnit是一个浏览器。仅仅因为你看不到它的渲染效果并不能使它更像浏览器 - 它只是意味着它是无头的。 – 2016-11-23 17:40:29

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

不再有效:ImportError:没有名为remote的模块 – 2017-11-28 21:31:02

10

我找到了答案,在https://stackoverflow.com/a/5518175/125170

As of the 2.0b3 release of the python client you can create an HTMLUnit webdriver via a remote connection like so:

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.

Note that you need to run the Selenium Java server for this to work, since HTMLUnit is implemented on the Java side.

-6

//在这种情况下,您可以使用HtmlUnitDriver。

 import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

//声明和初始化的HtmlUnitWebDriver

HtmlUnitDriver unitDriver = new HtmlUnitDriver(); 

//打开google.com网页

unitDriver.get("http://google.com"); 
+1

Downvote,这是针对java的。 OP要求python。 – Jeflopo 2015-05-02 17:50:07

相关问题