HTMLUNIT getformbyname without the form name specified on the website

I am trying to click a button on a website using HTMLUNIT i, following this guide http://htmlunit.sourceforge.net/gettingStarted.html , but this requires a form name. The website I'm trying to make has this 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 am trying to click the Yes button check box on a web page. (Remove Valdation). As you can see, there is no form name. 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 did not work

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

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

+6
source share
1 answer

Yes, you can get this XPath element this way:

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

Of course, if you have more elements with this action, you must correctly install XPath to select the element that you expect to receive. The getFirstByXPath method will return only one of all relevant results. If you want to get a lot of results, and then do further processing, you should go for the getByXPath method.

Let me know how it works.

+13
source

Source: https://habr.com/ru/post/910106/


All Articles