2017-08-08 20 views
0

我使用htmlunitnet.sourceforge.htmlunit模擬網絡瀏覽器。我嘗試登錄蒸汽網絡應用程序,但我遇到了問題。設置憑據後我想用click方法:Html單位沒有重定向點擊後

final WebClient webClient = new WebClient(); 
webClient.getOptions().setJavaScriptEnabled(true); 
webClient.getOptions().setRedirectEnabled(true); 
webClient.setCookieManager(new CookieManager()); 

final HtmlPage loginPage = webClient.getPage(loginPageConfiguration.getLoginPageUrl()); 
final HtmlTextInput user = loginPage.getHtmlElementById(loginPageConfiguration.getLoginInputId()); 
user.setText(loginCredentials.getUsername()); 
final HtmlPasswordInput password = loginPage.getHtmlElementById(loginPageConfiguration.getPasswordInputId()); 
password.setText(loginCredentials.getPassword()); 
final HtmlPage afterLoginPage = loginPage.getHtmlElementById(loginPageConfiguration.getLoginButtonId()).click(); 

在正常的瀏覽器succesfull登錄後重定向到http://store.steampowered.com/afterLoginPage仍是以前的登錄頁面。

回答

0

不知道頁面和憑據訪問我只能猜測。也許應用程序是一個單頁應用程序,用ajax代替視覺內容(也許與重定向結合)。由於ajax是異步的,因此它可能有助於在點擊之後和尋址頁面之前稍等一會兒。

通過檢查真實瀏覽器的http通信(通過使用開發人員工具或使用像Charles這樣的Web代理),也可能是瞭解發生了什麼的一個很好的起點。您可以將其與HtmlUnit完成的通信進行比較(例如啓用HttpClient連線日誌)。

另一個選項可能是一個JavaScript錯誤。請檢查您的日誌輸出。

+0

'的https:// store.steampowered.com/login'後sucesfull登錄它應該重定向到'HTTP:// store.steampowered.com /' – ByeBye

+0

我試着在循環等待,或加上'waitForBackgroundJavaScript'但沒有工作。還有'webClient.setAjaxController(新的NicelyResynchronizingAjaxController());' – ByeBye

+0

@ByeBye:Steam有一個安全功能並不是不可能的,它會禁用他們UI的自動化。 – Henrik