HTMLUNIT getformbyname with no form name specified in the website

穿精又带淫゛_ 提交于 2019-12-07 00:45:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9589802/htmlunit-getformbyname-with-no-form-name-specified-in-the-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!