htmlUnit 点击 link 按钮 - Java

htmlUnit clicking a link button - Java

编辑: 为了让整个事情变得清晰,我试图做的是让程序转到 http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS 并单击红色 "enter and vote" 按钮

我想做的是以编程方式访问网页并单击 href 按钮,如下所示:

<a href="http://www.ultimateprivateservers.com/index.php?a=in&amp;u=IkovPS&amp;sid=cSnJc3vgjV1P8rOe3l88Dv5ut1Wx1aBU" class="btn btn-danger">Enter and vote</a>

我用 htmlUnit 看了一些 tuts,但似乎无法正常工作。我究竟做错了什么?有人能指出我正确的方向吗?我对 java 不是很好,所以它会让人感到困惑。

这是我的代码:

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

public class HtmlUnitFormExample {
    public static void main(String[] args) throws Exception {
        WebClient webClient = new WebClient();
        HtmlPage page = webClient.getPage("http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS");

        HtmlLink enterAndVoteButton = 
                          page.getElementByName("btn btn-danger"); 
        page=enterAndVoteButton.click();

        HtmlDivision resultStatsDiv =
                                page.getFirstByXPath("//div[@id='vote_message_fail']");

        System.out.println(resultStatsDiv.asText()); 
        webClient.closeAllWindows();
    }
}

这是控制台日志:

    SEVERE: IOException when getting content for iframe: url=[http://a.tribalfusion.com/p.media/aPmQ0x0qPp4WYBPGZbE4PJZdodZanVdfb0bQjYrBeXaisRUvDUFB5WHn0mFBoRU7y1T3s5TUj2qfXmEjIYbYgUHBUoP7Cns7uptfG5Evl5teN5ABLpbbL0V7R1VF3XGjNmqJQ3FQ2WFJBW6Q2QEf1ScUMQdUOYtbuTPbx2G32XrnZcVmun4PQgQmnH4HQrXHBAMTAJplZd1Wp/3002246/adTag.html]
org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:188)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:178)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1313)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1230)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
    at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPageIfPossible(BaseFrameElement.java:184)
    at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPage(BaseFrameElement.java:122)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1993)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:238)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:475)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:342)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392)
    at HtmlUnitFormExample.main(HtmlUnitFormExample.java:7)
Caused by: org.apache.http.HttpException: Unsupported Content-Coding: none
    at org.apache.http.client.protocol.ResponseContentEncoding.process(ResponseContentEncoding.java:98)
    at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:139)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:200)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
    ... 14 more

Apr 18, 2015 5:28:37 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[name] attributeValue=[btn btn-danger]
    at com.gargoylesoftware.htmlunit.html.HtmlPage.getElementByName(HtmlPage.java:1747)
    at HtmlUnitFormExample.main(HtmlUnitFormExample.java:10)

非常感谢任何帮助。

我查看了该页面,并能够使用稍微不同的方法进行投票。如果您不想显示 window,我更喜欢使用 Selenium (http://www.seleniumhq.org/download/). I was able to use Selenium in Java to successfully cast a vote using the very crude code below. You can edit and optimize this code to your specific needs. I watched the whole process in an Internet Explorer driver but you could also use PhantomJS (http://phantomjs.org/download.html) 作为您的驱动程序。这是我的简单代码,setProperty 方法的第二个参数是你的驱动程序可执行文件的路径,这对你的计算机来说是唯一的(你也可以在 Selenium 下载页面上下载 IE 驱动程序):

import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class SeleniumTest()
{
    public static void main(String[] args) 
    {
        try
        {
            System.setProperty("webdriver.ie.driver"," IEDriverServer.exe");
            WebDriver driver = new InternetExplorerDriver();
            driver.get("http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS");
            Thread.sleep(3000); //use the wait as shown below
            WebElement button = driver.findElement(By.linkText("Enter and vote"));
            button.click();
            driver.close();
            driver.quit();
        }catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

等待页面加载的更好方法是这样的:

WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Enter and vote")));

您也可以使用 class 找到按钮,例如:

WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("btn-danger")));