dimanche 27 septembre 2015

Trying to login to a site using Java and HTML Unit

Below I've attached the full code, this is the part where it crashes HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("login");

The problem I seem to be having is getting the program to click the button. I right clicked on the button and used the inspect element in chrome to find the name of the button and then used getInputByName but I seem to be missing something.

**

    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

    public class GoogleRobotSearch {
     private String bUrl;

     public GoogleRobotSearch (String url) throws Exception {
      bUrl = url;
     }

     public void search () throws Exception {
      WebClient wb = new WebClient ();
      HtmlPage p = (HtmlPage) wb.getPage(bUrl);

      HtmlForm form = p.getFormByName("frmLogin");
      HtmlTextInput text = (HtmlTextInput) form.getInputByName("username");    

      HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("login");

      text.setValueAttribute("Ziplok Java");

      HtmlPage resultPage = (HtmlPage) button.click();
      System.out.println(resultPage.asText());



     }

     public static void main (String args[]) throws Exception {
      GoogleRobotSearch xyro = new GoogleRobotSearch ("http://www.pof.com/");
      xyro.search ();
     }
    }

**




Aucun commentaire:

Enregistrer un commentaire