尝试使用Java HtmlUnitDriver
的简单代码打开网页。我没有任何编译错误,但是driver.get(url)
从未打开任何网页。
这是我的 pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cpqtests.server</groupId>
<artifactId>cpq-selenium-server</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.48.2</version>
</dependency>
</dependencies>
</project>
我运行mvn clean install
,但库没有问题。 (使用IntelliJ IDEA)
这是我的Java代码 home.java :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class home {
private static final String GOOGLE = "https://www.google.com";
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver(true);
driver.get(GOOGLE);
System.out.println(((HtmlUnitDriver) driver).getBrowserVersion().getUserAgent());
System.out.println(((HtmlUnitDriver) driver).getCapabilities().toString());
System.out.println(((HtmlUnitDriver) driver).getPageSource());
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
运行该Java文件时,我得到以下输出
"C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.1\lib\idea_rt.jar=52129:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_192\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\rt.jar;C:\Users\emgonzal\IdeaProjects\cpqseleniumserver\target\classes;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-server\2.48.2\selenium-server-2.48.2.jar;C:\Users\emgonzal\.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\emgonzal\.m2\repository\org\bouncycastle\bcprov-jdk15on\1.48\bcprov-jdk15on-1.48.jar;C:\Users\emgonzal\.m2\repository\org\bouncycastle\bcpkix-jdk15on\1.48\bcpkix-jdk15on-1.48.jar;C:\Users\emgonzal\.m2\repository\mx4j\mx4j-tools\3.0.1\mx4j-tools-3.0.1.jar;C:\Users\emgonzal\.m2\repository\org\mortbay\jetty\servlet-api-2.5\6.1.9\servlet-api-2.5-6.1.9.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\jetty-repacked\9.2.13.v20150730\jetty-repacked-9.2.13.v20150730.jar;C:\Users\emgonzal\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\jetty-rc-repacked\5\jetty-rc-repacked-5.jar;C:\Users\emgonzal\.m2\repository\net\jcip\jcip-annotations\1.0\jcip-annotations-1.0.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-java\2.48.2\selenium-java-2.48.2.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver\2.48.2\selenium-chrome-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-remote-driver\2.48.2\selenium-remote-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\cglib\cglib-nodep\2.1_3\cglib-nodep-2.1_3.jar;C:\Users\emgonzal\.m2\repository\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-api\2.48.2\selenium-api-2.48.2.jar;C:\Users\emgonzal\.m2\repository\com\google\guava\guava\18.0\guava-18.0.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-edge-driver\2.48.2\selenium-edge-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\emgonzal\.m2\repository\org\apache\commons\commons-exec\1.3\commons-exec-1.3.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-htmlunit-driver\2.48.2\selenium-htmlunit-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\net\sourceforge\htmlunit\htmlunit\2.18\htmlunit-2.18.jar;C:\Users\emgonzal\.m2\repository\xalan\xalan\2.7.2\xalan-2.7.2.jar;C:\Users\emgonzal\.m2\repository\xalan\serializer\2.7.2\serializer-2.7.2.jar;C:\Users\emgonzal\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Users\emgonzal\.m2\repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar;C:\Users\emgonzal\.m2\repository\org\apache\httpcomponents\httpmime\4.5\httpmime-4.5.jar;C:\Users\emgonzal\.m2\repository\net\sourceforge\htmlunit\htmlunit-core-js\2.17\htmlunit-core-js-2.17.jar;C:\Users\emgonzal\.m2\repository\xerces\xercesImpl\2.11.0\xercesImpl-2.11.0.jar;C:\Users\emgonzal\.m2\repository\xml-apis\xml-apis\1.4.01\xml-apis-1.4.01.jar;C:\Users\emgonzal\.m2\repository\net\sourceforge\nekohtml\nekohtml\1.9.22\nekohtml-1.9.22.jar;C:\Users\emgonzal\.m2\repository\net\sourceforge\cssparser\cssparser\0.9.16\cssparser-0.9.16.jar;C:\Users\emgonzal\.m2\repository\org\w3c\css\sac\1.3\sac-1.3.jar;C:\Users\emgonzal\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\emgonzal\.m2\repository\org\eclipse\jetty\websocket\websocket-client\9.2.12.v20150709\websocket-client-9.2.12.v20150709.jar;C:\Users\emgonzal\.m2\repository\org\eclipse\jetty\jetty-util\9.2.12.v20150709\jetty-util-9.2.12.v20150709.jar;C:\Users\emgonzal\.m2\repository\org\eclipse\jetty\jetty-io\9.2.12.v20150709\jetty-io-9.2.12.v20150709.jar;C:\Users\emgonzal\.m2\repository\org\eclipse\jetty\websocket\websocket-common\9.2.12.v20150709\websocket-common-9.2.12.v20150709.jar;C:\Users\emgonzal\.m2\repository\org\eclipse\jetty\websocket\websocket-api\9.2.12.v20150709\websocket-api-9.2.12.v20150709.jar;C:\Users\emgonzal\.m2\repository\org\apache\httpcomponents\httpclient\4.5.1\httpclient-4.5.1.jar;C:\Users\emgonzal\.m2\repository\org\apache\httpcomponents\httpcore\4.4.3\httpcore-4.4.3.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-firefox-driver\2.48.2\selenium-firefox-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-ie-driver\2.48.2\selenium-ie-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\net\java\dev\jna\jna\4.1.0\jna-4.1.0.jar;C:\Users\emgonzal\.m2\repository\net\java\dev\jna\jna-platform\4.1.0\jna-platform-4.1.0.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-safari-driver\2.48.2\selenium-safari-driver-2.48.2.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-support\2.48.2\selenium-support-2.48.2.jar;C:\Users\emgonzal\.m2\repository\org\webbitserver\webbit\0.4.14\webbit-0.4.14.jar;C:\Users\emgonzal\.m2\repository\io\netty\netty\3.5.2.Final\netty-3.5.2.Final.jar;C:\Users\emgonzal\.m2\repository\org\seleniumhq\selenium\selenium-leg-rc\2.48.2\selenium-leg-rc-2.48.2.jar;C:\Users\emgonzal\.m2\repository\org\yaml\snakeyaml\1.8\snakeyaml-1.8.jar;C:\Users\emgonzal\.m2\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar" home
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
Capabilities [{browserName=htmlunit, javascriptEnabled=true, version=2.18, platform=XP, cssSelectorsEnabled=true}]
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head/>
<body/>
</html>
Page title is:
Process finished with exit code 0
我想念什么吗?除了安装maven并将其添加到路径以及Java jdk之外,我没有执行任何其他步骤。
我还没有从硒站点下载任何东西,因为我希望Maven能够处理依赖性。
如果有问题,我目前正在通过VPN连接运行。
我以前从未使用过硒,所以也许我错过了一些预配置吗?
答案 0 :(得分:0)
似乎问题出在VPN,但是我想加载的页面上的requirejs仍然导致超时,所以我最终使用WebClient
而不是WebDriver
来允许忽略js异常
public static void main(String[] args) throws IOException {
final WebClient webClient = new WebClient(BrowserVersion.CHROME, PROXY_HOST, PROXY_PORT);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
final CookieManager cm = webClient.getCookieManager();
HtmlPage currentPage = webClient.getPage(PAGE_URL);
HtmlForm loginForm = currentPage.getFormByName("loginform");
loginForm.getInputByName("username").type(Credentials.USERNAME);
loginForm.getInputByName("psword").type(Credentials.PASSWORD);
currentPage = currentPage.getElementById("log_in").click();;
System.out.println(currentPage.getTitleText());
System.out.println(currentPage.getUrl().toString());
try {
System.out.println("CSRF: "+ getCsrfToken(currentPage));
}catch (Exception e){
e.printStackTrace();
}
Set<Cookie> allCookies = cm.getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}
webClient.close();
}
private static String getCsrfToken(HtmlPage htmlPage) {
return htmlPage.executeJavaScript(TOKEN_VARIABLE).getJavaScriptResult().toString();
}