Joe Taylor
Joe Taylor

Reputation: 181

htmlUnit clicking a link button - Java

EDIT: To make this whole thing clear, what im trying to do it make the program go to http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS and click the red "enter and vote" button

What I'm trying to do is access a webpage programmatically and click a href button that goes like this:

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

I've looked at a few tuts with htmlUnit and I can't seem to get this working. What am I doing wrong? Could someone point me in the right direction? I'm not very good with java so it will get confusing.

Here is my code:

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();
    }
}

and here is the console log:

    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)

Any help is much appreciated.

Upvotes: 2

Views: 1232

Answers (1)

klib
klib

Reputation: 697

I took a look at the page and was able to cast a vote using a slightly different method. I prefer to use 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) as your driver if you do not want the window to show. Here is my simple code, the second argument of the setProperty method is the path to your driver executable this will be unique to your computer (you can download the IE driver on the Selenium downloads page as well):

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();
        }
    }
}

A better way to wait for the page to load would be like this:

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

You could also find the button using the class like:

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

Upvotes: 2

Related Questions