許家瑄
許家瑄

Reputation: 97

Java:Html unit click()

I'm parsing a website with Java and Html unit. Here's the button I want to click

<a id="ContentPlaceHolder1_btnSend" href='javascript:WebForm_DoPostBackWithOptions
    (new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$btnSend", "", true, "", "", false, true))'>
    <img src="../img/button12.gif" alt="">
</a>

and here's part of my code

public void parse_del(String delno){
    try
    {
        WebClient client = new WebClient(BrowserVersion.CHROME);

        client.getOptions().setCssEnabled(false);
        client.getOptions().setJavaScriptEnabled(false);
        //get first page
        HtmlPage currentPage = (HtmlPage)client.getPage("https://www.t-cat.com.tw/Inquire/Trace.aspx");
        //find inout area
        HtmlTextInput inputbox = (HtmlTextInput) currentPage.getElementById("ContentPlaceHolder1_txtQuery1");
        //set value
        inputbox.setValueAttribute(delno);
        //find the send button
        HtmlElement send = (HtmlElement) currentPage.getElementById("ContentPlaceHolder1_btnSend");
        //click
        HtmlPage newpage = send.click();
    }
    catch (Exception ex){
        System.out.println("ERROR !");
    }
}

However it still at the same page after click the button. I think the reason might be this line

client.getOptions().setJavaScriptEnabled(false);

Function click() cannot execute successfully because I enable the Javascript.

But if I delete this line, there will be tons of warning messages like

17, 2019 10:13:34 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
Warning: Obsolete content type encountered: 'application/x-javascript'.
17, 2019 10:13:34 com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet isValidCondition
Warning: Unhandled CSS condition type 'PREFIX_ATTRIBUTE_CONDITION'. Accepting it silently.
17, 2019 10:13:34 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
Warning: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: *:x).] sourceName=[https://www.t-cat.com.tw/js/jquery.js] line=[2] lineSource=[null] lineOffset=[0]
17, 2019 10:13:34  com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
Warning: Obsolete content type encountered: 'application/x-javascript'.
17, 2019 10:13:34 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
Warning: Obsolete content type encountered: 'application/x-javascript'.
17, 2019 10:13:34  com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
Warning: Obsolete content type encountered: 'application/x-javascript'.

and the whole program will stock at the beginning.

What should I do to fix the problem?

Upvotes: 2

Views: 210

Answers (1)

許家瑄
許家瑄

Reputation: 97

I solve it by Jsoup.

Document doc = Jsoup.connect(url)
                .data("__EVENTTARGET","ctl00$ContentPlaceHolder1$btnSend")
                .data("__EVENTARGUMENT:","")
                .data("q", "")
                .data("ctl00$ContentPlaceHolder1$txtQuery1", delno)
                .post();

I think it's a little bit like the request in scrapy.

Hope it could be helpful to someone confused.

Upvotes: 1

Related Questions