HtmlUnit - 从表单获取按钮

HtmlUnit - Getting button from form

我正在尝试使用 HtmlUnit 库登录 facebook:

String loginURL = "https://www.facebook.com/login.php?login_attempt=1"

final WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage(loginURL);
final HtmlForm form = page1.getForms().get(0);
webClient.setJavaScriptTimeout(45000);

final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute(login);
final HtmlPasswordInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute(password);
page1 = (HtmlPage) form.getInputByValue("Zaloguj się").click();

我检查了按钮值:

<input value="Zaloguj się" tabindex="4" type="submit" id="u_0_0">

在 运行 之后出现以下代码错误:

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[value] attributeValue=[Zaloguj się]

你能看到这里有什么错误吗? 感谢您的帮助:)

我尝试浏览 URL 提到的“https://www.facebook.com/login.php?login_attempt=1”,没有值为 [Zaloguj się] 的按钮。可能您的计算机使用另一种默认语言而不是英语。

我建议通过 ID 获取登录按钮,然后执行 click()。

page1.getElementById("loginbutton").click();

这是因为我看到登录按钮的 ID 为。

<button value="1" class="_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy" id="loginbutton" name="login" tabindex="1" type="submit">Log In</button>