HtmlUnit点击Href“图片”

时间:2017-03-26 12:37:27

标签: java html xpath href htmlunit

如何使用HtmlUnit点击此“图片”的任何想法?

<a href="play.php?choose=2">
<div id="champbox" style="background-image: url(http:xxx.jpg);float:right;"></div></a>

试图用HtmlAnchor和Xpath点击它,但是收到这样的错误:

java.lang.NullPointerException

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[a] attributeName=[href] attributeValue=[play.php?choose=2]

以下是重现问题的所有部分:

方:http://guessleague.net/#

您不需要帐户。

我的代码:

        //Webclient Options
        final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getCookieManager().setCookiesEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);


java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");


    // Cut out the Login (better Overview)

    HtmlAnchor anchor = page2.getAnchorByHref("play.php");
    HtmlPage page3 = anchor.click();
    System.out.println(page3.getTitleText());
    System.out.println(page3.getUrl());

    //To-Do: Click left or right Picture

    /*
    HtmlAnchor a1 = page3.getAnchorByHref("play.php?choose=2");
    System.out.println(a1.asXml());
    HtmlAnchor a2 = page3.getFirstByXPath("//a[@href='play.php?choose=2']");
    System.out.println(a2.asXml());

    a1.click();
    a2.click();
    */
    /*
    ScriptResult result = page3.executeJavaScript("vapi-client.js:41");
    result.getJavaScriptResult();
    System.out.println("result: "+ result);
    */

更新:并遇到新问题:

  

在访问之前检查您的浏览器..

尝试解决这个问题;)

2 个答案:

答案 0 :(得分:0)

不确定您是否正在谈论此问题,但如果您想更改光标以向某人显示您可以点击该图片:

.champbox {
  cursor:pointer;
}

答案 1 :(得分:0)

您的案例适用于最新版本/快照。

try (WebClient webClient = new WebClient()) {
    HtmlPage page = webClient.getPage("http://localhost:8080");
    HtmlAnchor a1 = page.getAnchorByHref("play.php?choose=2");
    System.out.println(a1.asXml());
    HtmlAnchor a2 = page.getFirstByXPath("//a[@href='play.php?choose=2']");
    System.out.println(a2.asXml());
}
{{1}}