public static void main(String[] args) throws Exception { LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); File file = new File("cookie.file"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); Set<Cookie> cookies = (Set<Cookie>) in.readObject(); in.close(); WebClient wc = new WebClient(); Iterator<Cookie> i = cookies.iterator(); while (i.hasNext()) { wc.getCookieManager().addCookie(i.next()); } HtmlPage p = wc.getPage("http://google.com"); ObjectOutput out = new ObjectOutputStream(new FileOutputStream("cookie.file")); out.writeObject(wc.getCookieManager().getCookies()); out.close(); }
WebClient client = new WebClient(); HtmlPage page = client.getPage("http://stackoverflow.com"); // create a submit button - it doesn't work with 'input' HtmlElement button = page.createElement("button"); button.setAttribute("type", "submit"); // append the button to the form HtmlElement form = ...; form.appendChild(button); // submit the form page = button.click();
@Test public void submittingForm() throws Exception { final WebClient webClient = new WebClient(); // Get the first page final HtmlPage page1 = webClient.getPage("http://some_url"); // Get the form that we are dealing with and within that form, // find the submit button and the field that we want to change. final HtmlForm form = page1.getFormByName("myform"); final HtmlSubmitInput button = form.getInputByName("submitbutton"); final HtmlTextInput textField = form.getInputByName("userid"); // Change the value of the text field textField.setValueAttribute("root"); // Now submit the form by clicking the button and get back the second page. final HtmlPage page2 = button.click(); webClient.closeAllWindows(); }
public static void main(String[] args) throws Exception { // turn off htmlunit warnings java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://stackoverflow.com"); System.out.println(page.getTitleText()); }
WebClient client = new WebClient(); Page page = client.getPage("http://stackoverflow.com/users/flair/97901.json"); WebResponse response = page.getWebResponse(); if (response.getContentType().equals("application/json")) { String json = response.getContentAsString(); Map<String, String> map = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {}.getType()); System.out.println(map.get("displayName")); // Benju }
URL url = new File("test.html").toURI().toURL(); WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage(url); OutputStream os = null; try{ os = new FileOutputStream("test.pdf"); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(page,url.toString()); renderer.layout(); renderer.createPDF(os); } finally{ if(os != null) os.close(); }
public class SavePage { public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { WebClient client = new WebClient(); HtmlPage page = client.getPage("https://www.google.co.in"); FileWriter writer = new FileWriter("d:\\google.html"); writer.write(page.asXml()); //Save page on file system writer.close(); } }
WebClient wc = new WebClient(); URL url = new URL("http://www.oogly.co.uk/"); HtmlPage page = (HtmlPage) wc.getPage(url); PrintWriter printWriter = new PrintWriter(new FileWriter(FILE_NAME)); List anchors = page.getAnchors();
@SuppressWarnings("serial") public class GuestbookServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/plain"); try (WebClient webClient = new WebClient()) { final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); resp.getWriter().println(page.getTitleText()); } } }
try (WebClient webClient = new WebClient()) { HtmlPage page = webClient.getPage("http://localhost/test.html"); Object o = page.getByXPath("//span[@data-reactid='.3.1.0.0.1:1.0.1.0.1:$0.$=12:$2.2']"); System.out.println(o); }
public static void main(String[] args) { String pageURL = args[1]; WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage(pageURL); String pageContents = page.asText(); // Save the resulting page to a file }
import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class Main { public static void main(String[] args) throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://www.mp3juices.cc/download/sEhy-RXkNo0/mp3/rihanna_-_man_down/"); webClient.closeAllWindows(); System.out.println(page.getElementById("download_link", false).getAttribute("href")); } }
@Test public void homePage() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText()); final String pageAsXml = page.asXml(); assertTrue(pageAsXml.contains("<body class=\"composite\">")); final String pageAsText = page.asText(); assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols")); }
@Test public void getElements() throws Exception { try (final WebClient webClient = new WebClient()) { final HtmlPage page = webClient.getPage("http://some_url"); final HtmlDivision div = page.getHtmlElementById("some_div_id"); final HtmlAnchor anchor = page.getAnchorByName("anchor_name"); } }
WebClient webClient = new WebClient(BrowserVersion.CHROME); String url = "https://github.com/login"; webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); HtmlPage page = webClient.getPage(url); DomElement form = (DomElement) page.querySelector("form"); System.out.println(form.asXml()); webClient.close();
String html = "<html>Your html here</html>"; WebClient client = new WebClient(); MockWebConnection connection = new MockWebConnection(); connection.setDefaultResponse(html); client.setWebConnection(connection); HtmlPage page = client.getPage(someUrl);
`@Test public void getElements() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://some_url"); final HtmlDivision div = page.getHtmlElementById("some_div_id"); final HtmlAnchor anchor = page.getAnchorByName("anchor_name"); webClient.closeAllWindows(); }`
final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://www.facebook.com"); final HtmlForm form = page1.getFormByName("login_form"); final HtmlSubmitInput button = form.getInputsByValue("Log in"); final HtmlTextInput textField = form.getInputByName("email"); textField.setValueAttribute("jon@jon.com"); final HtmlTextInput textField = form.getInputByName("pass"); textField.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();
public void submittingForm() throws Exception { WebClient webClient = new WebClient(); HtmlPage page1 = webClient.getPage("http://some_url"); HtmlForm form = page1.getFormByName("myform"); HtmlSubmitInput button = form.getInputByName("submitbutton"); HtmlTextInput textField = form.getInputByName("userid"); textField.setValueAttribute("root"); HtmlPage page2 = button.click(); }
final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://www.facebook.com"); final HtmlForm form = (HtmlForm) page1.getElementById("login_form"); final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0); final HtmlTextInput textField = form.getInputByName("email"); textField.setValueAttribute("jon@jon.com"); final HtmlPasswordInput textField2 = form.getInputByName("pass"); textField2.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();