Skip to content

Instantly share code, notes, and snippets.

@apaci
Created June 1, 2012 17:41
Show Gist options
  • Save apaci/2853917 to your computer and use it in GitHub Desktop.
Save apaci/2853917 to your computer and use it in GitHub Desktop.
HTMLUnit Thingy
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlHiddenInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class cza {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
String address = "https://www.com:1234/1/var?blabla=blabla&var2=blablabla";
HtmlPage currentPage = webClient.getPage(address);
final HtmlForm form = currentPage.getFirstByXPath("//form[@action='/login']");
HtmlTextInput username = (HtmlTextInput) currentPage.getElementById("login_email");
HtmlPasswordInput password = (HtmlPasswordInput) currentPage.getElementById("login_password");
username.setText("username@username.com");
password.setText("password");
HtmlSubmitInput firstButton = (HtmlSubmitInput) currentPage.getElementById("login_submit");
HtmlPage newPage = firstButton.click();
System.out.println(newPage.asText());
//System.out.println("***button test start***");
///////////// problems start after this line
HtmlForm form2 = newPage.getFirstByXPath("//form[@action='authorize']");
//
//form2.getInputByName("allow");
HtmlPage finalPage = form2.getButtonByName("allow").click();
//
//HtmlButton button = newPage.getElementByName("allow");
//HtmlPage page = button.click() ;
///////
System.out.println(finalPage.asText());
System.out.println("***button test end***");
}
}
@apaci
Copy link
Author

apaci commented Jun 1, 2012

It now prints the new page. Hurray. However, still can't click the button.

@apaci
Copy link
Author

apaci commented Jun 2, 2012

These also fail:

//form2.getInputByName("allow_access");
//HtmlPage finalPage =  form2.getButtonByName("allow_access").click();
//HtmlPage finalPage = form2.getInputByName("allow_access").click();
//HtmlPage finalPage = form2.getInputByValue("Allow").click();
HtmlPage finalPage = newPage.getElementByName("Allow").click();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment