简体   繁体   English

HtmlUnit - 登录网站后页面不重定向

[英]HtmlUnit - After login to website the page doesn't redirect

I'm trying to use HtmlUnit to get data fom a web site, so far i managed to enter the data needed in order to login (username & password).我正在尝试使用 HtmlUnit 从 web 站点获取数据,到目前为止,我设法输入了登录所需的数据(用户名和密码)。

The problem begins with the fact that the website login form dosent have a submit button, instead there is an href that has an onClick JavaScript function (onclick="login_submit()) that verify data and then call window.location = "index.php"; The problem begins with the fact that the website login form dosent have a submit button, instead there is an href that has an onClick JavaScript function (onclick="login_submit()) that verify data and then call window.location = "index.php";

So i tried clicking the href using:所以我尝试使用以下方法单击href:

HtmlAnchor anchor = page.getAnchorByText("כניסה");
page = anchor.click();

and tried:并尝试:

page.executeJavaScript(javaScriptCode);

but the page variable dosent get the expected page, it got the same url as before clicking and there is no redirection.但是页面变量dosent得到了预期的页面,它得到了与点击前相同的url并且没有重定向。 Once the login_submit() function is called there is a label that indicates " login.." but nothing happens from there.一旦login_submit() function ,就会有一个 label 指示“登录..”,但从那里没有任何反应。

Here is the code:这是代码:

// javascript login_submit() -- this is not my website and not my JS code so ive deleted some things:

function login_submit()
{
    if(!LOGIN_SUBMIT_PROCESSED)
    {
        var approved = true;
        var admin_user_username_field = document.getElementById('admin_user_username');
        var admin_user_username_error = document.getElementById('admin_user_username_error');
        var admin_user_password_field = document.getElementById('admin_user_password');
        var admin_user_password_error = document.getElementById('admin_user_password_error');

        admin_user_username_field.className = "login_form_field_input_text";
        admin_user_username_error.style.display = 'none';
        admin_user_password_field.className = "login_form_field_input_text";
        admin_user_password_error.style.display = 'none';

        if(admin_user_username_field.value.length == 0)
        {
            //do something
        }
        if(admin_user_password_field.value.length == 0)
        {
            //do something
        }
        if(approved)
        {
            LOGIN_SUBMIT_PROCESSED = true;
            document.getElementById("login_form_options").style.display = "none";
            document.getElementById("login_form_process").style.display = "";

            var http_request = new XHConn();
            http_request.connect(
                "login_submit.php?cache="+string_unique(),
                "POST",
                "form_anti_bot_code="+encodeURIComponent(form_anti_bot_code_field.value)
                + "&admin_user_username="+encodeURIComponent(admin_user_username_field.value)
                + "&admin_user_password="+encodeURIComponent(admin_user_password_field.value),
                function(response,callback_data)
                {
                    if(response.readyState == 4)
                    {
                        if(response.status == 200)
                        {
                            //alert(response.responseText);
                            try
                            {
                                var json_response = JSON.parse(response.responseText);

                                if(json_response["error_code"] == "0")
                                {
                                    window.location = "index.php";
                                }
                                else
                                {
                                    //do something
                                }
                            }
                            catch(error)
                            {
                                alert(error);
                            }
                        }

                        LOGIN_SUBMIT_PROCESSED = false;
                    }
                },
                null
            );
        }
    }
}

Java code: Java代码:

public static void main(String[] args) throws Exception {

    WebClient webClient = new WebClient();
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setCssEnabled(true);
    //webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setRedirectEnabled(true);


    HtmlPage page = (HtmlPage) webClient
            .getPage(url);
    HtmlForm form = page.getFormByName("login_form");

    form.getInputByName("admin_user_username").setValueAttribute("XXXXX"); 
    HtmlInput passWordInput = form.getInputByName("admin_user_password");
    passWordInput.removeAttribute("disabled");
    passWordInput.setValueAttribute("XXXXXX"); 

    HtmlAnchor anchor = page.getAnchorByText("Login");
    page = anchor.click();
    System.out.println(page.getBody().asText());
    webClient.close();
}

I know for a fact after some testing that the login data is ok, href is clicked and there is an indicator that login is being proccesed.经过一些测试,我知道一个事实,即登录数据正常,单击 href 并且有一个指示正在登录。 the main problem is that its not redirecting and dont get the "index.php" page.主要问题是它没有重定向并且没有得到“index.php”页面。

Try this and it should do the trick: 试试这个,它应该可以解决问题:

WebWindow window = page.getEnclosingWindow(); WebWindow窗口= page.getEnclosingWindow();

This might do the trick:这可能会奏效:

final HtmlPage page2 = page.getElementById("Login_Button").click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM