[英]How do I use the HtmlUnit driver with Selenium through the Python bindings?
I'm using WebDriver through the Python bindings located on Google's site .我正在通过位于 Google 站点上的 Python 绑定使用 WebDriver。 According to the documentation here , it supports four browsers: Chrome, IE, Firefox, and HtmlUnit.
根据这里的文档,它支持四种浏览器:Chrome、IE、Firefox 和 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
.我可以导入使用Firefox司机
from selenium.firefox.webdriver import WebDriver
,并使用Chrome司机from selenium.chrome.webdriver import WebDriver
。
There isn't a comparable HtmlUnit module.没有可比的 HtmlUnit 模块。 How do I import the HtmlUnit driver?
如何导入 HtmlUnit 驱动程序?
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.
您还可以将 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 端实现的。
HtmlUnit is a Java library so the only choice for non-java WebDriver bindings is to use a RemoteWebDriver. HtmlUnit 是一个 Java 库,因此非 Java WebDriver 绑定的唯一选择是使用 RemoteWebDriver。 You will need to start a Selenium Server and connect to it specifying the HtmlUnit as desired browser.
您需要启动一个 Selenium 服务器并连接到它,指定 HtmlUnit 作为所需的浏览器。
I am not very familiar with Python, but according to http://code.google.com/p/selenium/wiki/PythonBindings it 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")
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')))
I agree with the answer above, but the server must be opened before that.我同意上面的答案,但必须在此之前打开服务器。 After downloading the server and the htmlunitdriver, cmd should be opened in the same file.
下载服务器和htmlunitdriver后,cmd应该在同一个文件中打开。 and this should be written to cmd.
这应该写入cmd。
java -cp "htmlunit-driver-2.47.1-jar-with-dependencies.jar; selenium-server-standalone-3.141.0.jar" org.openqa.grid.selenium.GridLauncherV3
Of course the version may be different.当然版本可能不一样。 Update the code accordingly.
相应地更新代码。 Also the code I use for htmlunitdriver in python:
还有我在 python 中用于 htmlunitdriver 的代码:
from selenium import webdriver
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get('http://www.google.com')
// 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.