1
Java / Re: How to mimic a web browser using Jsoup?
« on: August 25, 2015, 08:31:43 AM »
I think you'll find the HtmlUtil library useful:
Code: [Select]
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
final HtmlPage facebook = webClient.getPage("http://www.facebook.com");
final HtmlForm form = (HtmlForm) facebook.getElementById("login_form");
//Filling out the form
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0);
final HtmlTextInput textField = (HtmlTextInput) facebook.getElementById("email");
textField.setValueAttribute("email@email.com");
final HtmlPasswordInput textField2 = (HtmlPasswordInput) facebook.getElementById("pass");
textField2.setValueAttribute("password");
//Logging in:
HtmlPage loggedin = button.click();
System.out.println("logged in");