HTMLUNIT getformbyname,网站中未指定表单名称 [英] HTMLUNIT getformbyname with no form name specified in the website

查看:317
本文介绍了HTMLUNIT getformbyname,网站中未指定表单名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTMLUNIT在网站上找到一个按钮我按照本教程 http:// htmlunit .sourceforge.net / gettingStarted.html 但它需要一个表单名称。我正在尝试做的网站有这个页面来源。

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();

我试过这个但是没有用

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

我相信您可以使用xpath查找表单名称?

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

推荐答案

是的,你可以通过XPath这样获得这个元素:

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

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

当然,如果您有更多元素与该操作,您应该正确设置XPath以选择元素你希望得到。方法 getFirstByXPath 将仅返回所有匹配结果中的一个。如果你想得到很多结果,然后做进一步的处理,你应该去方法 getByXPath

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.

这篇关于HTMLUNIT getformbyname,网站中未指定表单名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆