HtmlUnit,无法重定向到其他网站

时间:2017-03-05 15:26:57

标签: java htmlunit


我想使用SteamAccount登录csgolounge。我正在使用HtmlUnit。
我做了什么:
1.通过SteamAccount登录时单击csgolounge 2.在steamcommunity.com上填写用户名和密码,然后单击提交按钮
3.从Steam移动应用程序(手动)填写令牌代码,然后单击登录按钮

现在我应该被重定向到我应该登录的csgolounge。但我仍然在steamcommunity网站上,并且无法重定向。有我的代码:

public class Logger {

    public void logIn(String username, String password) {
        final WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
        try {
            // login to csgolounge.com using SteamAccount
            HtmlPage page = webClient.getPage(Constants.CSGOLOUNGE_URL);
            HtmlAnchor loginAnchor = (HtmlAnchor) page.getByXPath("//div[@id='status']/a[2]").get(0);
            HtmlPage pageAfterClick = loginAnchor.click();  
            webClient.waitForBackgroundJavaScript(10000L);
            HtmlAnchor steamLoginAnchor = (HtmlAnchor) pageAfterClick.getByXPath("//div[@id='logIn']/form[1]/div[1]/div[last()]/a[1]").get(0);
            HtmlPage steamLoginPage = steamLoginAnchor.click();
            webClient.waitForBackgroundJavaScript(10000L);
            // login on steamcomminity.com
            HtmlTextInput usernameInput = (HtmlTextInput) steamLoginPage.getElementById("steamAccountName");            
            HtmlPasswordInput passwordInput = (HtmlPasswordInput) steamLoginPage.getElementById("steamPassword");
            HtmlSubmitInput loginButton = (HtmlSubmitInput) steamLoginPage.getElementById("imageLogin");            
            usernameInput.setValueAttribute(username);
            passwordInput.setValueAttribute(password);
            HtmlPage steamPageAfterClickLogIn = loginButton.click();
            webClient.waitForBackgroundJavaScript(10000L);
            // I need to write token code from steam mobile app
            HtmlTextInput codeAfterLoginInput = (HtmlTextInput) steamPageAfterClickLogIn.getElementById("twofactorcode_entry");
            HtmlDivision codeSubmitDiv = (HtmlDivision) steamPageAfterClickLogIn.getByXPath("//div[@id='login_twofactorauth_buttonset_entercode']/div[1]").get(0);
            codeAfterLoginInput.setValueAttribute(getLoginCode());
            // and there I should be redirected to csgolounge, where I should be logged on
            HtmlPage pageAfterSendingCode = codeSubmitDiv.click();
            webClient.waitForBackgroundJavaScriptStartingBefore(10000L);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    private String getLoginCode() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please write ur code:");
        return scanner.nextLine();
    }


    public static void main(String[] args) {
        Logger logger = new Logger();
        logger.logIn(Constants.USERNAME, Constants.PASSWORD);
    }

}

我犯了错误?

EDIT1: 也许onclick动作有问题,因为codeSubmitDiv上没有onclick(),但是当我使用Web浏览器点击它时,它会以某种方式工作。检查屏幕imgur.com/a/QYp56

0 个答案:

没有答案