无法使用java selenium HtmlUnit打开任何页面(maven安装)



尝试使用javaHtmlUnitDriver打开一个网页的简单代码。我没有任何编译错误,但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 FilesJavajdk1.8.0_192binjava.exe" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2018.3.1libidea_rt.jar=52129:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2018.3.1bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_192jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_192jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_192jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_192jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_192jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_192jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_192jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_192jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_192jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_192jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_192jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_192jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_192jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_192jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_192jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_192jrelibjce.jar;C:Program FilesJavajdk1.8.0_192jrelibjfr.jar;C:Program FilesJavajdk1.8.0_192jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_192jrelibjsse.jar;C:Program FilesJavajdk1.8.0_192jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_192jrelibplugin.jar;C:Program FilesJavajdk1.8.0_192jrelibresources.jar;C:Program FilesJavajdk1.8.0_192jrelibrt.jar;C:UsersemgonzalIdeaProjectscpqseleniumservertargetclasses;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-server2.48.2selenium-server-2.48.2.jar;C:Usersemgonzal.m2repositorycombeustjcommander1.48jcommander-1.48.jar;C:Usersemgonzal.m2repositoryorgbouncycastlebcprov-jdk15on1.48bcprov-jdk15on-1.48.jar;C:Usersemgonzal.m2repositoryorgbouncycastlebcpkix-jdk15on1.48bcpkix-jdk15on-1.48.jar;C:Usersemgonzal.m2repositorymx4jmx4j-tools3.0.1mx4j-tools-3.0.1.jar;C:Usersemgonzal.m2repositoryorgmortbayjettyservlet-api-2.56.1.9servlet-api-2.5-6.1.9.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumjetty-repacked9.2.13.v20150730jetty-repacked-9.2.13.v20150730.jar;C:Usersemgonzal.m2repositoryjavaxservletjavax.servlet-api3.1.0javax.servlet-api-3.1.0.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumjetty-rc-repacked5jetty-rc-repacked-5.jar;C:Usersemgonzal.m2repositorynetjcipjcip-annotations1.0jcip-annotations-1.0.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-java2.48.2selenium-java-2.48.2.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-chrome-driver2.48.2selenium-chrome-driver-2.48.2.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-remote-driver2.48.2selenium-remote-driver-2.48.2.jar;C:Usersemgonzal.m2repositorycglibcglib-nodep2.1_3cglib-nodep-2.1_3.jar;C:Usersemgonzal.m2repositorycomgooglecodegsongson2.3.1gson-2.3.1.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-api2.48.2selenium-api-2.48.2.jar;C:Usersemgonzal.m2repositorycomgoogleguavaguava18.0guava-18.0.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-edge-driver2.48.2selenium-edge-driver-2.48.2.jar;C:Usersemgonzal.m2repositorycommons-iocommons-io2.4commons-io-2.4.jar;C:Usersemgonzal.m2repositoryorgapachecommonscommons-exec1.3commons-exec-1.3.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-htmlunit-driver2.48.2selenium-htmlunit-driver-2.48.2.jar;C:Usersemgonzal.m2repositorynetsourceforgehtmlunithtmlunit2.18htmlunit-2.18.jar;C:Usersemgonzal.m2repositoryxalanxalan2.7.2xalan-2.7.2.jar;C:Usersemgonzal.m2repositoryxalanserializer2.7.2serializer-2.7.2.jar;C:Usersemgonzal.m2repositorycommons-collectionscommons-collections3.2.1commons-collections-3.2.1.jar;C:Usersemgonzal.m2repositoryorgapachecommonscommons-lang33.4commons-lang3-3.4.jar;C:Usersemgonzal.m2repositoryorgapachehttpcomponentshttpmime4.5httpmime-4.5.jar;C:Usersemgonzal.m2repositorynetsourceforgehtmlunithtmlunit-core-js2.17htmlunit-core-js-2.17.jar;C:Usersemgonzal.m2repositoryxercesxercesImpl2.11.0xercesImpl-2.11.0.jar;C:Usersemgonzal.m2repositoryxml-apisxml-apis1.4.01xml-apis-1.4.01.jar;C:Usersemgonzal.m2repositorynetsourceforgenekohtmlnekohtml1.9.22nekohtml-1.9.22.jar;C:Usersemgonzal.m2repositorynetsourceforgecssparsercssparser.9.16cssparser-0.9.16.jar;C:Usersemgonzal.m2repositoryorgw3ccsssac1.3sac-1.3.jar;C:Usersemgonzal.m2repositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;C:Usersemgonzal.m2repositoryorgeclipsejettywebsocketwebsocket-client9.2.12.v20150709websocket-client-9.2.12.v20150709.jar;C:Usersemgonzal.m2repositoryorgeclipsejettyjetty-util9.2.12.v20150709jetty-util-9.2.12.v20150709.jar;C:Usersemgonzal.m2repositoryorgeclipsejettyjetty-io9.2.12.v20150709jetty-io-9.2.12.v20150709.jar;C:Usersemgonzal.m2repositoryorgeclipsejettywebsocketwebsocket-common9.2.12.v20150709websocket-common-9.2.12.v20150709.jar;C:Usersemgonzal.m2repositoryorgeclipsejettywebsocketwebsocket-api9.2.12.v20150709websocket-api-9.2.12.v20150709.jar;C:Usersemgonzal.m2repositoryorgapachehttpcomponentshttpclient4.5.1httpclient-4.5.1.jar;C:Usersemgonzal.m2repositoryorgapachehttpcomponentshttpcore4.4.3httpcore-4.4.3.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-firefox-driver2.48.2selenium-firefox-driver-2.48.2.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-ie-driver2.48.2selenium-ie-driver-2.48.2.jar;C:Usersemgonzal.m2repositorynetjavadevjnajna4.1.0jna-4.1.0.jar;C:Usersemgonzal.m2repositorynetjavadevjnajna-platform4.1.0jna-platform-4.1.0.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-safari-driver2.48.2selenium-safari-driver-2.48.2.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-support2.48.2selenium-support-2.48.2.jar;C:Usersemgonzal.m2repositoryorgwebbitserverwebbit.4.14webbit-0.4.14.jar;C:Usersemgonzal.m2repositoryionettynetty3.5.2.Finalnetty-3.5.2.Final.jar;C:Usersemgonzal.m2repositoryorgseleniumhqseleniumselenium-leg-rc2.48.2selenium-leg-rc-2.48.2.jar;C:Usersemgonzal.m2repositoryorgyamlsnakeyaml1.8snakeyaml-1.8.jar;C:Usersemgonzal.m2repositorycommons-codeccommons-codec1.10commons-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并将其添加到路径以及javajdk之外,我没有做任何额外的步骤。

此外,我还没有从selenium站点下载任何内容,因为我希望maven能够处理依赖关系。

我目前正在通过VPN连接运行,如果这很重要的话。

我以前从未使用过硒,所以可能我错过了一些预配置?

问题似乎是VPN,但我想加载的页面上的requirej仍然导致超时,所以我最终使用了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();
}

相关内容

  • 没有找到相关文章

最新更新