0
我試圖單擊使用HtmlUnit 2.23的登錄按鈕,使用DomElement
或executeJavaScript
,但它們都不起作用。有沒有其他的方式來使它工作?這是代碼:單擊HtmlUnit不工作的按鈕
@Test
public void noLogin() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
//see http://stackoverflow.com/questions/3600557/turning-htmlunit-warnings-off/18563576#18563576
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
//see http://stackoverflow.com/questions/18556200/htmlunit-button-click
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
final HtmlPage loginPage = webClient.getPage("https://sandbox.paydirekt.de/account/#/login");
loginPage.getElementById("username").setAttribute("value", "no");
loginPage.getElementById("password").setAttribute("value", "login");
//final HtmlPage welcomePage = ((DomElement)loginPage.getFirstByXPath("//button[@type='submit']")).click();
//see http://stackoverflow.com/questions/14474580/use-htmlunit-doesnt-work-button-click
//ScriptResult result = loginPage.executeJavaScript("javascript:document.getElementsByName('loginForm')[0].submit();");
ScriptResult result = loginPage.executeJavaScript("javascript:document.getElementsByClassName('loginBox__loginButton button button--full')[0].click()");
Page page = result.getNewPage();
if (page.isHtmlPage()) {
HtmlPage welcomePage = ((HtmlPage)page);
System.out.println(welcomePage.asText());
Assert.assertTrue(welcomePage.asText().contains("Benutzername oder Passwort nicht korrekt."));
}
webClient.close();
}