【问题标题】:HtmlUnit getform() form not found找不到 HtmlUnit getform() 表单
【发布时间】:2016-01-07 04:46:01
【问题描述】:

我是 HtmlUnit 的新手 .. 我想登录 https://trash-mail.com/posteingang/

我的代码:

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        WebClient webClient = new WebClient(BrowserVersion.CHROME);

        HtmlPage page1 = webClient.getPage("https://www.trash-mail.com/posteingang/");

        HtmlForm form = page1.getFormByName("inbox-form");

我收到以下错误:

runtimeError: message=[指定了一个无效或非法的选择器(选择器:'select option:selected' 错误:无效的选择器:select option:selected)。] sourceName=[https://www.trash-mail.com/js/jquery-1.9.1.min.js.pagespeed.ce.OXdUuknp4M.js]line=[4] lineSource=[空] lineOffset=[0] 线程“主”com.gargoylesoftware.htmlunit.ElementNotFoundException 中的异常:elementName=[form] attributeName=[name] attributeValue=[inbox-form] 在 com.gargoylesoftware.htmlunit.html.HtmlPage.getFormByName(HtmlPage.java:647)

你有什么想法吗?

问候, MaddinCoy

在这个网站上做这些事情是合法的。

【问题讨论】:

    标签: java html htmlunit


    【解决方案1】:

    在那个 HTML 页面上,我没有看到任何具有该名称的表单 - inbox-forminbox-form 是表单的id,而不是名称。

    <form id="inbox-form" action="#" method="post" enctype="multipart/form-data" class="form-horizontal" autocomplete="off">
    

    添加name 属性以形成或使用getElementById 方法之一。

    这个页面只有一个表单,所以你也可以试试这个语法HtmlForm form = page.getForms().get(0);

    编辑 - 我尝试运行您的代码,问题出现在HtmlPage page = webClient.getPage("https://www.trash-mail.com/posteingang/");

    错误消息 - SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: 'select option:selected' error: Invalid selector: select option:selected).] sourceName=[https://www.trash-mail.com/js/jquery-1.9.1.min.js.pagespeed.ce.OXdUuknp4M.js] line=[4] lineSource=[null] lineOffset=[0]

    这似乎是 HtmlUnit 的 Jquery 支持问题或该脚本或用法中的一些错误,htmlUnitbugs

    我的建议 - 1. 不要从 main 方法抛出异常,尝试处理这些异常 2. 调试你的每一行代码

    我们似乎无能为力,因为您无法更改该页面。

    【讨论】:

    • 感谢您的回复!如果我使用'final HtmlForm form = (HtmlForm) page.getElementById("inbox-form");'我收到一个错误...你能给我一个例子如何找到表格并在这个表格中输入一些东西吗?谢谢...
    • 什么错误?通过首先将名称属性添加到表单来尝试您的旧代码。
    • ` runtimeError: message=[指定了无效或非法的选择器(选择器:'select option:selected' 错误:无效的选择器:select option:selected)。] sourceName=[trash-mail.com/js/… line= [4] lineSource=[null] lineOffset=[0] `网页不是我的项目不能在webserver上更改代码
    • 我已经修改了答案,试试其他语法HtmlForm form = page.getForms().get(0);
    猜你喜欢
    • 2021-02-16
    • 1970-01-01
    • 2019-09-20
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多