2017-03-26 69 views
0

任何想法如何點擊這個「形象」與HtmlUnit點擊的HtmlUnit HREF「圖像」

<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); 
    */ 

更新:並獲得了新的問題有:

檢查之前訪問你的瀏覽器..

揣摩了這一點;)

回答

0

不確定你是否在討論這個問題,但是如果你想討論ge光標以向其他人展示您可以點擊圖片:

.champbox { 
    cursor:pointer; 
} 
+0

不是在談論這一點。用HtmlUnit搜索點擊這個「圖像」的方法。像image.click();但是,謝謝;) – BiFi

+0

對不起,我不是那麼好:( –

0

您的情況適用於最新版本/快照。

<a href="play.php?choose=2"> 
    <div id="champbox" style="background-image: url(http:xxx.jpg);float:right;"></div> 
</a> 
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()); 
} 
+0

首先謝謝你,我嘗試了兩種方式,仍然得到錯誤: 爲更好的概述:[照片](https://www.pic- upload.de/view-32910787/Unbenannt.png.html) – BiFi

+0

然後請註明您的[完成](https://stackoverflow.com/help/mcve)的情況下 –

+0

做,對不起只是想保持它的簡單和清晰 – BiFi