Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLUNIT getformbyname with no form name specified in the website

I'm trying to clcik a button on a website using HTMLUNIT i followed this tutorial http://htmlunit.sourceforge.net/gettingStarted.html but it requires a form name. The website I'm trying to do has this page source.

<form method="post" action="doDelete">
     Are you sure you want to delete 'Apple?'?
 <input name="Submit" value="Yes" class="submit-button" type="submit" />
 </form>

I'm trying to click the "Yes" button validation box on the webpage.(Delete Valdation) As you can see there is no form name supplied. Here is my code.

 final WebClient webClient = new WebClient();
        final HtmlPage page1 = webClient.getPage("http://ma.some-site.com:8080/user/mike/delete");



        List<HtmlForm> formlist = (List<HtmlForm>) page1.getForms();
       System.out.println(formlist.toString());

        final HtmlForm form = page1.getFormByName("myform"); <---Problem here
       final HtmlSubmitInput button = form.getInputByName("Submit");
       button.click();
        webClient.closeAllWindows();

I tried this but does not work

  final HtmlForm form =   page1.getFormByName(formlist.get(1).getLocalName());

I believe you can use xpath to find the form name?

like image 866
shinra tensei Avatar asked Mar 06 '12 18:03

shinra tensei


1 Answers

Yes, you can get this element by XPath this way:

final HtmlForm form = page1.getFirstByXPath("//form[@action='doDelete']");

Of course, if you have more elements with that action you should properly set the XPath to select the element you expect to get. The method getFirstByXPath will return only one of all the matching results. If you want to get many results and then do further processing then you should go for method getByXPath.

Let me know how that works.

like image 195
Mosty Mostacho Avatar answered Nov 15 '22 11:11

Mosty Mostacho