HTML单元登录



基本上我正在尝试使用HTML单元执行登录。

然而,登录名以输入下一个按钮输入用户名,然后将表单操纵,并应输入密码。当我做button.click()时,我的问题发生了,页面获取第一个表格,而不是应输入密码的第二个表单

public void search() throws Exception {
    WebClient wb = new WebClient();
    HtmlPage p = wb.getPage(
            "https://account.booking.com/sign-in?op_token=EgVvYXV0aCJHChQ2Wjcyb0hPZDM2Tm43emszcGlyaBIJYXV0aG9yaXplGhpodHRwczovL2FkbWluLmJvb2tpbmcuY29tLyoCe31CBGNvZGUqDDCgqZHe5rMjOgBCAA");
    // HtmlPage p = (HtmlPage) wb.getPage(this.bUrl);
    List<HtmlForm> form = p.getForms();
    form.get(0).getInputByName("loginname").setValueAttribute("1234567");
    HtmlForm fm = form.get(0);
    System.out.println(form.get(0).getInputByName("loginname").getValueAttribute().toString());
    List<Object> button = fm.getByXPath("//button[@type='submit']");
    HtmlButton bt = (HtmlButton) button.get(0);
    System.out.println(p.asText() + "n+_________________");
    bt.click();
    System.out.println(p.asText());
}

bt.click()

之前和之后的输出显示为相同
1234567
Booking.com Account
This website uses cookies. Click here for more information.
Close
Sign In to Manage Your Property
Username
1234567
Next
Having trouble signing in?
Questions about your property or the Extranet? Visit the Partner Help Center or ask another partner on the Partner Forum.
Add your property to Booking.com
Create a partner account to list and manage your property.
Register
By clicking "Allow access" you authorize Extranet to use your Booking.com account info according to Extranet Terms of service.
+_________________
Booking.com Account
This website uses cookies. Click here for more information.
Close
Sign In to Manage Your Property
Username
Enter your username
Next
Having trouble signing in?
Questions about your property or the Extranet? Visit the Partner Help Center or ask another partner on the Partner Forum.
Add your property to Booking.com
Create a partner account to list and manage your property.
Register
By clicking "Allow access" you authorize Extranet to use your Booking.com account info according to Extranet Terms of service.

对不起,但是您的代码基于对HTML和HTMLUNIT的基本误解。

HtmlPage p = wb.getPage(.....

检索(HTML(页面。此页面显示在浏览器窗口内(HTMLUNIT中的相同(。如果您与此页面上的元素进行交互,例如

form.get(0).getInputByName("loginname").setValueAttribute("1234567");

或更好的

form.get(0).getInputByName("loginname").type("1234567");

这些元素在那里发生了变化,结果整个页面都会改变。但:单击提交按钮是一个完全不同的故事。在这种情况下,浏览器(和HTMLUNIT也(将HTTP请求发送到服务器并恢复新的HTMLPAGE。通常此页面显示在同一窗口内。

在htmlunit中,这是由单击方法的返回值反映的 - 返回值是新页面。只要您就不会将此值分配给页面变量并在此新页面上执行下一步,您仍在使用旧页面。

btw:入门htmlunit页面上有一个注释的示例。

到目前为止,最简单的形式/提交处理。但是今天,认为(实际上很多位(的想法更加复杂,因为大多数页面都基于JavaScript(例如Ajax(。

建议:如果您通过私人邮件向我发送了一些凭据,我可以尝试帮助您根据htmlunit进行此登录工作。

建议2:尝试学习和理解与网络相关的所有技术知识,而没有此信息。

最新更新