@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 insertScore(String user, boolean win){ final WebClient webClient = new WebClient(); try { final HtmlPage page1 = webClient.getPage("http://mywebpage/insert.php?user="+user+"&win="+win); } catch (IOException | FailingHttpStatusCodeException ex) { Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex); } webClient.closeAllWindows(); }
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 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(); }`
public void xpath() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); //get list of all divs final List<?> divs = page.getByXPath("//div"); //get div which has a 'name' attribute of 'John' final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']/button").get(0); webClient.closeAllWindows(); }
import java.net.URL; import com.gargoylesoftware.htmlunit.*; import com.gargoylesoftware.htmlunit.html.*; public class HtmlUnitAnchorExample { public static void main(String[] args) throws Exception { String html = "<html><head><title>HTMLUNIT TEST</title></head><body>" + "" + "<span class=\"followButtonActions\" id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1]. [1].[1]\"> "+ " <a class=\"Button FollowButton followButtonFollow\" role=\"button\" href=\"javascript:;\" id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0\"> "+ " <span id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0.[0]\"> "+ " <span id=\".reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0.[0].[0]\">Follow</span> </span> "+ " </a> "+ "</span>" + "</body></html>"; WebClient client = new WebClient(); HtmlPage page = HTMLParser.parseHtml(new StringWebResponse(html, new URL("http://example.com")), client.getCurrentWindow()); System.out.println("Page title: "+page.getTitleText()); final HtmlAnchor myAnchor = page.getFirstByXPath("//a[@id='.reactRoot[0].[0].[1].[2].{userprofile0001}.[1].[1].[1].[1].0']"); System.out.println("Anchor found: "+myAnchor ); final HtmlPage newPage = myAnchor .click(); System.out.println("newPage: "+newPage); client.closeAllWindows(); } }
XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools @Test public void xpath() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); //get list of all divs final List<?> divs = page.getByXPath("//div"); //get div which has a 'name' attribute of 'John' final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0); webClient.closeAllWindows(); }
client.closeAllWindows();
public boolean Login(String user, String pass, String url) { try { final WebClient webClient = new WebClient(); // Get the first page final HtmlPage page1 = webClient.getPage(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("oauthAuthorizeForm"); final HtmlSubmitInput button = form.getInputByName("authorize"); final HtmlTextInput textField = form.getInputByName("session_login"); final HtmlPasswordInput textField2 = form.getInputByName("session_password"); // Change the value of the text field textField.setValueAttribute(user); textField2.setValueAttribute(pass); // Now submit the form by clicking the button and get back the second page. final HtmlPage page2 = button.click(); String newurl = page2.getUrl().toString(); String oauthVerifier = newurl.split("oauth_verifier=")[1].split("&")[0]; accessToken = oauthService.getOAuthAccessToken(requestToken, oauthVerifier); webClient.closeAllWindows(); logined = true; return true; } catch (Exception ex) { Logger.getLogger(ClassLinkedIn.class.getName()).log(Level.SEVERE, null, ex); } return false; }
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.google.com"); HtmlInput searchBox = page.getElementByName("q"); searchBox.setValueAttribute("htmlunit"); HtmlSubmitInput googleSearchSubmitButton = page.getElementByName("btnG"); // sometimes it's "btnK" page=googleSearchSubmitButton.click(); HtmlDivision resultStatsDiv = page.getFirstByXPath("//div[@id='resultStats']"); System.out.println(resultStatsDiv.asText()); // About 309,000 results webClient.closeAllWindows(); } }
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.google.com"); HtmlInput searchBox = page.getElementByName("q"); searchBox.setValueAttribute("htmlunit"); HtmlSubmitInput googleSearchSubmitButton = page.getElementByName("btnG"); // sometimes it's "btnK" page=googleSearchSubmitButton.click(); HtmlDivision resultStatsDiv = page.getFirstByXPath("//div[@id='resultStats']"); System.out.println(resultStatsDiv.asText()); // About 301,000 results webClient.closeAllWindows(); } }
public static void main(String... args) throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://some_url"); final HtmlForm form = page1.getFormByName("myform"); final HtmlSubmitInput button = form.getInputByName("submitbutton"); final HtmlTextInput textField = form.getInputByName("userid"); textField.setValueAttribute("root"); final HtmlPage page2 = button.click(); webClient.closeAllWindows(); }
@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(); }
@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(); }
@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(); }
@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 void login() 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("username"); // Do similar for password and that's all // Now submit the form by clicking the button and get back the second page. final HtmlPage page2 = button.click(); webClient.closeAllWindows(); }
&& TEXT.equals(text2)); webClient.closeAllWindows();