Login to LinkedIn using HtmlUnit and navigate to Connections Web page

杀马特。学长 韩版系。学妹 提交于 2019-12-23 06:01:37

问题


Currently I am trying to login to LinkedIn using HtmlUnit 2.20. But I am not able to login. Below is my code.

public static void Login(String username, String password) {
         final WebClient webClient = new WebClient(BrowserVersion.CHROME);
        try {

            final HtmlPage page = webClient.getPage "https://www.linkedin.com/secure/login");

            final HtmlForm form = page.getForms().get(0);
            final HtmlSubmitInput button = form.getInputByName("signin");
            final HtmlTextInput emailBtn = form.getInputByName("session_key");
            final HtmlPasswordInput passBtn = form.getInputByName("session_password");

            emailBtn.setValueAttribute(username);
            passBtn.setValueAttribute(password);

            final HtmlPage page2 = button.click();
            System.out.println(page2.getWebResponse().getContentAsString());

        } catch (Exception ex ){
            ex.printStackTrace();
        } 

    }

Could you please assist in finding what is wrong with my code and how do I navigate to another page after I login.


回答1:


Try this...

Here i found solution for linkedin login......

   try {
        String url = "https://www.linkedin.com/uas/login?goback=&trk=hb_signin";
        final WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setCssEnabled(false);

        final HtmlPage loginPage = webClient.getPage(url);
        //Get Form By name 
        final HtmlForm loginForm = loginPage.getFormByName("login");
        final HtmlSubmitInput button = loginForm.getInputByName("signin");
        final HtmlTextInput usernameTextField = loginForm.getInputByName("session_key");
        final HtmlPasswordInput passwordTextField = loginForm.getInputByName("session_password");
        usernameTextField.setValueAttribute(userName);//your Linkedin Username
        passwordTextField.setValueAttribute(password);//Your Linkedin Password
        final HtmlPage responsePage = button.click();
        String htmlBody = responsePage.getWebResponse().getContentAsString();
        System.out.println(htmlBody);
    } catch (Exception ex) {
        ex.printStackTrace();
    }


来源:https://stackoverflow.com/questions/36127360/login-to-linkedin-using-htmlunit-and-navigate-to-connections-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!