Я пытаюсь войти в yahoo.com, используя HtmlUnit . Но это не работает, когда моя программа пытается нажать кнопку «Войти». Мой код:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import java.io.IOException;
import java.net.URL;
public class MyBrowser {
public void homePage() throws Exception {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
webClient.setRefreshHandler(new RefreshHandler() {
public void handleRefresh(Page page, URL url, int arg) throws IOException {
System.out.println("handleRefresh");
}
});
HtmlPage page = (HtmlPage) webClient.getPage("https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.done=http%3A%2F%2Fwww.yahoo.com%2F");
HtmlForm form = page.getFormByName("login_form");
form.getInputByName("login").setValueAttribute("@@@@@@"); // works OK
form.getInputByName("passwd").setValueAttribute("@@@@@@"); // works OK
page = (HtmlPage) form.getInputByValue("Sign In").click(); // doesn't work
webClient.closeAllWindows();
}
}
Ошибка:
com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[value] attributeValue=[Sign In]
at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByValue(HtmlForm.java:737)
Форма:
<form method="post" action="https://login.yahoo.com/config/login?" autocomplete="" name="login_form" onsubmit="return hash2(this)">
...
<div id="submit">
<button type="submit" id=".save" name=".save" class="primaryCta" tabindex="5"> Sign In </button>
</div>
</form>