HtmlUnit - 如何获得正确的表单?

时间:2015-08-11 11:17:24

标签: java

我正在尝试通过HtmlUnit登录account.mojang.com/login。我似乎无法找到要登录的表单,所以我遍历页面上的所有表单,如果他们有登录按钮,它将预先形成一切。我怎样才能得到我要找的表格? (这是代码,但它不起作用。)

@SuppressWarnings("resource")
public static void nk(){
    String username = Sniper.username;
    String email = Sniper.email;
    String password = Sniper.pass;
    System.out.println("Sniping!");
    final WebClient web = new WebClient();
    try{
        final HtmlPage page1 = web.getPage("https://account.mojang.com/login");
        for(HtmlForm loginForm: page1.getForms()){
            if(loginForm.getInputByName("Log in")!=null){
                final List<HtmlInput> loginButton = loginForm.getInputsByValue("Log in");
                final HtmlTextInput emailEnter = loginForm.getInputByName("E-mail");
                emailEnter.setValueAttribute(email);
                final HtmlTextInput passbutton = loginForm.getInputByName("Password");
                passbutton.setValueAttribute(password);
                final HtmlPage page2 = loginButton.get(0).click();
                System.out.println(page2);
                changeUsername1(page2.getAnchorByText("Change").click(), username, password);
            }}
        }catch(Exception e){
            e.printStackTrace();
            sniped = true;
        }
    }
    @SuppressWarnings({ "resource", "unused" })
    public static void changeUsername1(Page page1,String username, String password){
        final WebClient web = new WebClient();
        try{
            final HtmlPage page = web.getPage(page1.getUrl());
            for(HtmlForm form :page.getForms()){
                if(form.getInputsByValue("Change name")!=null){
                    final List<HtmlInput> button = form.getInputsByValue("Change name");
                    final HtmlTextInput textField = form.getInputByName("New profile name");
                    textField.setValueAttribute(username);
                    final HtmlTextInput textField1 = form.getInputByName("Password");
                    textField1.setValueAttribute(password);
                    final HtmlPage page2 = button.get(0).click();
                    form.getButtonByName("Yes").click();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            sniped = true;
        }
        sniped = true;
    }

1 个答案:

答案 0 :(得分:1)

您使用的是哪种浏览器?使用Firebug或一个此类工具搜索表单。以下是该页面上的表格 - 使用Safari检查工具进行搜索。

<form action="/login" method="post" accept-charset="utf-8" enctype="application/x-www-form-urlencoded" id="loginForm"><input type="hidden" name="authenticityToken" value="60e3d81d00a7ed6e8cb192c52fcddfb9bdfe322d">