使用 HtmlUnit 虚拟 Web 浏览器提交 +爪哇

发布于 2024-10-31 00:51:04 字数 2172 浏览 1 评论 0原文

我正在尝试使用 HtmlUnit 登录 yahoo.com。但当我的程序尝试单击“登录”按钮时,它不起作用。我的代码是:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import java.io.IOException;
import java.net.URL;

public class MyBrowser {

    public void homePage() throws Exception {


        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        webClient.setThrowExceptionOnScriptError(false);

        webClient.setRefreshHandler(new RefreshHandler() {
            public void handleRefresh(Page page, URL url, int arg) throws IOException {
                System.out.println("handleRefresh");
            }

        });

        HtmlPage page = (HtmlPage) webClient.getPage("https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.done=http%3A%2F%2Fwww.yahoo.com%2F");
        HtmlForm form = page.getFormByName("login_form");

        form.getInputByName("login").setValueAttribute("@@@@@@");  // works OK
        form.getInputByName("passwd").setValueAttribute("@@@@@@"); // works OK
        page = (HtmlPage) form.getInputByValue("Sign In").click(); // doesn't work

        webClient.closeAllWindows();
    }

}

错误:

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[value] attributeValue=[Sign In]
        at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByValue(HtmlForm.java:737)

表格是:

<form method="post" action="https://login.yahoo.com/config/login?" autocomplete="" name="login_form" onsubmit="return hash2(this)">
    ...
    <div id="submit">
        <button type="submit" id=".save" name=".save" class="primaryCta" tabindex="5"> Sign In </button>
    </div>
</form>

I'm trying to sign in to yahoo.com using HtmlUnit. But it doesn't work when my program trying to click "Sign In" button. My code is:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import java.io.IOException;
import java.net.URL;

public class MyBrowser {

    public void homePage() throws Exception {


        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        webClient.setThrowExceptionOnScriptError(false);

        webClient.setRefreshHandler(new RefreshHandler() {
            public void handleRefresh(Page page, URL url, int arg) throws IOException {
                System.out.println("handleRefresh");
            }

        });

        HtmlPage page = (HtmlPage) webClient.getPage("https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.done=http%3A%2F%2Fwww.yahoo.com%2F");
        HtmlForm form = page.getFormByName("login_form");

        form.getInputByName("login").setValueAttribute("@@@@@@");  // works OK
        form.getInputByName("passwd").setValueAttribute("@@@@@@"); // works OK
        page = (HtmlPage) form.getInputByValue("Sign In").click(); // doesn't work

        webClient.closeAllWindows();
    }

}

Error:

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[value] attributeValue=[Sign In]
        at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByValue(HtmlForm.java:737)

Form is:

<form method="post" action="https://login.yahoo.com/config/login?" autocomplete="" name="login_form" onsubmit="return hash2(this)">
    ...
    <div id="submit">
        <button type="submit" id=".save" name=".save" class="primaryCta" tabindex="5"> Sign In </button>
    </div>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-11-07 00:51:04

我确实对 HtmlUnit 一无所知,但从错误消息来看,它似乎搜索某个具有 value 属性的元素,而您的按钮没有这样的属性。也许byName之类的?它不是 元素,而是

编辑:我找到了 Javadoc,似乎我猜对了方法名称:-p

I do know nothing about HtmlUnit, but from the error message it looks like the it searches for some element with a value attribute, and your button has no such attribute. Maybe byName or such? And it is not a <input> element, but a <button> element, so maybe something like getButtonByName(".save")?

Edit: I found the Javadoc, and seems I guessed the method name right :-p

翻了热茶 2024-11-07 00:51:04

Yahoo 登录表单提交按钮具有名称规范,而不是值规范。您需要按名称获取输入。我建议你改变:

page = (HtmlPage) form.getInputByValue("Sign In").click();

...到...

page = (HtmlPage) form.getInputByName("Sign In").click();

The Yahoo login form submit button has a name specification, not a value specification. You need to get the input by name. I suggest that you change:

page = (HtmlPage) form.getInputByValue("Sign In").click();

...to...

page = (HtmlPage) form.getInputByName("Sign In").click();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文