嗨,這是我第一次使用HtmlUnit [version 2.31],我正嘗試登錄到一個(gè)網(wǎng)頁(yè)。這是HTML:<body> <div id="login"> <div id="header"> User Log In </div> <div id="error">Enter your credentials to login</div> <table> <tr> <th>Username</th> <td><input type="text" id="username" /></td> </tr> <tr> <th>Password</th> <td><input type="password" id="password" /></td> </tr> </table> <div id="buttons"> <input type="button" value="Login" id="button" onclick="login();" /> </div> </div></body>這是我的代碼: WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setUseInsecureSSL(true); try{ HtmlPage page = webClient.getPage(url); String pageContent = page.asText(); System.out.println(pageContent); HtmlButtonInput button = page.getFirstByXPath("//input[@type = 'button']"); //I'm new to XPath, but I think this works okay HtmlTextInput name = (HtmlTextInput) page.getElementById("username"); HtmlPasswordInput pwd = (HtmlPasswordInput) page.getElementById("password"); System.out.println(name.getSelectedText()); name.setValueAttribute(username); pwd.setValueAttribute(password); System.out.println(name.getSelectedText()); HtmlPage loggedInPage = button.click(); String pageContent2 = loggedInPage.asText(); System.out.println("after logged in"); System.out.println(pageContent2);}兩頁(yè)(登錄之前和登錄之后)都一樣打印出來(lái)。所以我在這里一定做錯(cuò)了。任何幫助將不勝感激。
2 回答

鳳凰求蠱
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
我建議您嘗試設(shè)置:
webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setRedirectEnabled(true);
添加回答
舉報(bào)
0/150
提交
取消