使用Web客户端以没有提交按钮的形式登录网站

时间:2019-02-08 11:54:26

标签: java htmlunit

我想在需要我登录的网站上抓取一些信息。

我遇到过HtmlUnit,我已经能够找到登录表单,填写电子邮件和密码输入。我的问题是我无法提交此表单,因为没有提交按钮。

我已经尝试创建一个临时按钮,但是它不起作用。遵循了question

试图将提交输入定位为锚点,执行Java脚本等,但无济于事。

    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setCssEnabled(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getOptions().setRedirectEnabled(true);

    // disable caching
    webClient.getCache().setMaxSize(0);

    // Get the first page
    final HtmlPage page = webClient.getPage("https://www.jpdi.pt");

    webClient.waitForBackgroundJavaScript(10000);

    final HtmlForm form = page.getHtmlElementById("login_form");

    form.getInputByName("login_email").setValueAttribute("my_email");
    HtmlInput passWordInput = form.getInputByName("login_password");

    passWordInput.setValueAttribute("my_password");

    //wait a little
    Thread.sleep(2000);

    ScriptResult result= page.executeJavaScript("document.getElementById('submit_login').click()");

    final HtmlPage secondPage  = (HtmlPage) page.getWebClient().getCurrentWindow().getEnclosedPage();

    System.out.println(secondPage.asText());

所打印的页面是带有两个输入的原始页面,而不是我期望的页面。.有什么想法吗?

0 个答案:

没有答案