使用 HtmlUnit 虚拟 Web 浏览器提交 +爪哇
我正在尝试使用 HtmlUnit 登录 yahoo.com。但当我的程序尝试单击“登录”按钮时,它不起作用。我的代码是:
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>
I'm trying to sign in to yahoo.com using HtmlUnit. But it doesn't work when my program trying to click "Sign In" button. My code is:
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();
}
}
Error:
com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[value] attributeValue=[Sign In]
at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByValue(HtmlForm.java:737)
Form is:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确实对 HtmlUnit 一无所知,但从错误消息来看,它似乎搜索某个具有
value
属性的元素,而您的按钮没有这样的属性。也许byName
之类的?它不是元素,而是
编辑:我找到了 Javadoc,似乎我猜对了方法名称:-p
I do know nothing about HtmlUnit, but from the error message it looks like the it searches for some element with a
value
attribute, and your button has no such attribute. MaybebyName
or such? And it is not a<input>
element, but a<button>
element, so maybe something likegetButtonByName(".save")
?Edit: I found the Javadoc, and seems I guessed the method name right :-p
Yahoo 登录表单提交按钮具有名称规范,而不是值规范。您需要按名称获取输入。我建议你改变:
...到...
The Yahoo login form submit button has a name specification, not a value specification. You need to get the input by name. I suggest that you change:
...to...