如何通过 Python 绑定将 HtmlUnit 驱动程序与 Selenium 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4081724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:09:35  来源:igfitidea点击:

How do I use the HtmlUnit driver with Selenium through the Python bindings?

pythonseleniumwebdriver

提问by Chris B.

I'm using WebDriver through the Python bindings located on Google's site. According to the documentation here, it supports four browsers: Chrome, IE, Firefox, and HtmlUnit. I can import the Firefox driver using from selenium.firefox.webdriver import WebDriver, and the Chrome driver using from selenium.chrome.webdriver import WebDriver.

我正在通过位于 Google 站点上的 Python 绑定使用 WebDriver 。根据这里的文档,它支持四种浏览器:Chrome、IE、Firefox 和 HtmlUnit。我可以使用 导入 Firefox 驱动程序from selenium.firefox.webdriver import WebDriver,使用from selenium.chrome.webdriver import WebDriver.

There isn't a comparable HtmlUnit module. How do I import the HtmlUnit driver?

没有可比的 HtmlUnit 模块。如何导入 HtmlUnit 驱动程序?

回答by Sergii Pozharov

HtmlUnit is a Java library so the only choice for non-java WebDriver bindings is to use a RemoteWebDriver. You will need to start a Selenium Server and connect to it specifying the HtmlUnit as desired browser.

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

I am not very familiar with Python, but according to http://code.google.com/p/selenium/wiki/PythonBindingsit should look something like:

我对 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")

回答by Alexey Kuzminich

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

回答by Keith

I found the answer at https://stackoverflow.com/a/5518175/125170

我在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:

从 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.

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

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

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

回答by Shailesh

// You can use HtmlUnitDriver in this case.

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

       import org.openqa.selenium.htmlunit.HtmlUnitDriver;

// Declaring and initialising the HtmlUnitWebDriver

// 声明并初始化 HtmlUnitWebDriver

    HtmlUnitDriver unitDriver = new HtmlUnitDriver();

// open google.com webpage

// 打开 google.com 网页

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