首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中使用HtmlUnit单击HtmlUnit

在Java中使用HtmlUnit单击HtmlUnit
EN

Stack Overflow用户
提问于 2018-12-20 07:52:19
回答 2查看 1.7K关注 0票数 1

我目前正在开发一个系统。我需要点击使用Java的网站按钮。所以我使用的是HtmlUnit库。有一个名为https://tempmail.ninja/的网站可以生成临时电子邮件。我需要的是程序点击生成按钮在tempmail.ninja和它生成一个临时电子邮件。但问题是它不是点击。不是生成电子邮件。

这是我试过的代码,

代码语言:javascript
复制
    try
    {
       WebClient webClient = new WebClient(BrowserVersion.CHROME);
       webClient.getOptions().setCssEnabled(true);
       webClient.getOptions().setJavaScriptEnabled(true);
       webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
       webClient.getOptions().setThrowExceptionOnScriptError(false);
       webClient.getOptions().setUseInsecureSSL(true);
       webClient.getCookieManager().setCookiesEnabled(true);
       HtmlPage page = webClient.getPage("https://tempmail.ninja");

       //Here is button id. Instead of this I used HtmlAnchor, HtmlSubmitInput and etc.
       //But any of those didn't work         
       HtmlButton htmlButton = page.getHtmlElementById("generaEmailTemporal");
       htmlButton.click();

       webClient.waitForBackgroundJavaScript(5000 * 2);   

       //Print the generated email but Currently nothing display
       HtmlTextInput htmlTextInput = (HtmlTextInput) page.getElementById("emailtemporal");
       System.out.println(htmlTextInput.getText());

       webClient.close();

    } catch(ElementNotFoundException | FailingHttpStatusCodeException | IOException ex) {
       Logger.getLogger(WebTesting.class.getName()).log(Level.SEVERE, null, ex);
    }

下面是按钮的HTML代码。我用的是检查元件。

代码语言:javascript
复制
<p class="text-center" id="btnGeneraEmailTemporal">
<button class="btn btn-labeled btn-primary" id="generaEmailTemporal" type="button">
<span class="btn-label"><i class="fa fa-hand-o-right" aria-hidden="true"></i></span> Generate Temp Mail
</button>
</p>

我是HtmlUnit的新手。有人能帮我吗?我真的很感激。谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-21 02:46:11

我已经通过使用Selenium Web驱动程序解决了这个问题。使用selenium单击按钮并生成电子邮件并将其传递给HtmlUnit。

票数 0
EN

Stack Overflow用户

发布于 2018-12-20 08:16:05

.click()返回更改的页面。所以你应该这样做:

代码语言:javascript
复制
   HtmlButton htmlButton =  page.getHtmlElementById("generaEmailTemporal");
   HtmlPage pageAfterClick = (HtmlPage)htmlButton.click();
   webClient.waitForBackgroundJavaScript(5000 * 2);      
   System.out.println(pageAfterClick.asXml()); // often displaying the page-source is more useful during development than .asText()

由于waitForBackgroundJavaScript(..)是实验性的,并不总是工作,所以我更喜欢轮询,直到出现预期的文本。

代码语言:javascript
复制
private static final int AJAX_MAX_TRIES_SECONDS = 30;
private static final int ONE_SEC_IN_MILLISEC = 1000;

/** Waits until the given 'text' appeared or throws an
 * WaitingForAjaxTimeoutException if the 'text' does not appear before we timeout.
 * @param page
 * @param text The text which indicates that ajax has finished updating the page
 * @param waitingLogMessage Text for the log-output. Should indicate where in the code we are, and what are we waiting for
 * @throws WaitingForAjaxTimeoutException
 */
public static void waitForAjaxCallWaitUntilTextAppears(//
        @Nonnull final HtmlPage page, //
        @Nonnull final String text, //
        @Nonnull final String waitingLogMessage)  {
    LOGGER.debug("_5fd3fc9247_ waiting for ajax call to complete ... [" + waitingLogMessage + "]");
    final StringBuilder waitingdots = new StringBuilder("   ");
    for (int i = 0; i < AJAX_MAX_TRIES_SECONDS; i++) {

        if (page.asText().contains(text)) {
            waitingdots.append(" ajax has finished ['").append(text).append("' appeared]");
            LOGGER.debug("_8cd5a34faf_ " + waitingdots);
            return;
        }
        waitingdots.append('.');
        final long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < ONE_SEC_IN_MILLISEC) {
            try {
                o.wait(ONE_SEC_IN_MILLISEC);
            }
            catch (final InterruptedException e) {
                // ignore
            }
        }

    }
    LOGGER.debug("_de5091bc9e_ "
            + waitingdots.append(" ajax timeout ['").append(text).append("' appeared NOT]").toString());
    LOGGER.debug("_f1030addf1_ page source:\n" + page.asXml());
    throw new RuntimeException("_ec3df4f228_");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53864468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档