English 中文(简体)
HtmlUnit HtmlImage Input.click () not working?
原标题:HtmlUnit HtmlImageInput.click() not working?

我对HtmlUnit还有点陌生,尝试使用HtmlImageInput.click()提交表单遇到些问题。当我调用此方法后,似乎什么都没有发生,没有表单提交,没有与服务器的往返,我检查后也没发现有任何问题。该方法立即返回当前页面。

在图像投入中,没有 Java字事件手。 它只是一个老的面包车图像投入,没有任何特殊作用。 这些投入最初是在装满该网页时提供给残疾人的,然后随着用户与该网页的某些自动自动自动协调机制要素进行互动,便得以投入使用。 但是,到我点击这些投入时,它已经能够做到,因此,我不认为它是一个美国退休人员协会的问题。

有人知道发生了什么吗?可运行的源代码如下。

Thanks, Matthew

import java.io.*;
import java.util.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import org.w3c.dom.*;

public class Test {

public static void main(String args[]) {

    try {
        WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
        webClient.setThrowExceptionOnScriptError(false);
        HtmlPage page = webClient.getPage("http://us.megabus.com");
        System.out.println("got the page");
        HtmlForm form = page.getFormByName("ctl01");
        System.out.println("got the form");
        HtmlSelect select = form.getSelectByName("SearchAndBuy1$ddlLeavingFrom");
        select.click();
        System.out.println("clicked the select");
        HtmlOption option = select.getOptionByValue("13");
        option.click();
        System.out.println("clicked the option...going to sleep");
        try { Thread.sleep(15000); } catch(InterruptedException e) {}
        select = form.getSelectByName("SearchAndBuy1$ddlTravellingTo");
        select.click();
        System.out.println("clicked the select 2");
        option = select.getOptionByValue("37");
        option.click();
        System.out.println("clicked the option 2...going to sleep");
        try { Thread.sleep(15000); } catch(InterruptedException e) {}
        HtmlImage image = (HtmlImage)page.getElementById("SearchAndBuy1_imgOutboundDate");
        image.click();
        System.out.println("clicked the image");
        String month = "April";
        String date = "09";
        HtmlTable table = (HtmlTable)page.getElementById("SearchAndBuy1_calendarOutboundDate");
        HtmlTableRow row = ((HtmlTable)table.getCellAt(0, 0).getChildElements().iterator().next()).getRow(0);
        String monthString = row.getCell(1).getTextContent();
        monthString = monthString.substring(0, monthString.indexOf(   ));
        while(!monthString.equals(month)) {
            row.getCell(2).getChildElements().iterator().next().click();
            System.out.println("clicked to go to the next month");
            try { Thread.sleep(15000); } catch(InterruptedException e) {}
            table = (HtmlTable)page.getElementById("SearchAndBuy1_calendarOutboundDate");
            row = ((HtmlTable)table.getCellAt(0, 0).getChildElements().iterator().next()).getRow(0);
            monthString = row.getCell(1).getTextContent();
            monthString = monthString.substring(0, monthString.indexOf(   ));
        }
        DomNodeList<HtmlElement> aList = table.getElementsByTagName("a");
        for (int i = 0; i < aList.size(); i++) {
            HtmlAnchor anchor = (HtmlAnchor)aList.get(i);
            if (anchor.getAttribute("title").equals(DomElement.ATTRIBUTE_NOT_DEFINED) || anchor.getAttribute("title").equals(DomElement.ATTRIBUTE_VALUE_EMPTY))
                throw new RuntimeException("DomElement ATTRIBUTE_NOT_DEFINED or ATTRIBUTE_VALUE_EMPTY");
            if (anchor.getAttribute("title").equals(month + " " + date)) {
                anchor.click();
                try { Thread.sleep(15000); } catch(InterruptedException e) {}
                break;
            }
        }
        HtmlImageInput imageInput = (HtmlImageInput)page.getElementByName("SearchAndBuy1$btnSearch");
        page = (HtmlPage)imageInput.click();
        System.out.println("clicked search button");

    } catch(FailingHttpStatusCodeException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    } catch(ElementNotFoundException e) {
        e.printStackTrace();
    } catch(IndexOutOfBoundsException e) {
        e.printStackTrace();
    }
}
}
问题回答

那个图片不是一个输入框,它只是一张普通的图片。

<img id="SearchAndBuy1_imgOutboundDate" disabled="disabled" alt="calendar"
    CausesValidation="False" src="images/icon_calendar.gif" style="border-width:0px;" />

那里没有指定JS句柄,因此它们必须在其他地方附加,而且似乎它们在页面底部。

Sys.Application.add_init(function() {
    $create(AjaxControlToolkit.PopupControlBehavior,
       {"PopupControlID":"SearchAndBuy1_panelOutboundDate","Position":3,"dynamicServicePath":"/default.aspx","id":"SearchAndBuy1_pceImageOutboundDate"}, null, null, $get("SearchAndBuy1_imgOutboundDate"));

}; }; 请翻译成中文:});

当您的程序点击图像时,没有表单提交,只有一次AJAX调用(可能),所以您是正确的,您不会得到一个新页面。但是,正如您的代码证明的那样(我刚刚用调试器运行了它),HtmlPage的内容已经改变,因为它现在包含了日历小部件,您可以从中获取细节。

有时候很难确定何时会得到一个全新的HtmlPage,但通常只有在浏览器中看到全新的页面时才会出现。我从未尝试将HtmlUnit用于像Gmail这样的东西,但我猜想您可能只会处理一个HtmlPage对象,并且所有操作都在其中进行。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签