Java:Html单元点击()

问题描述 投票:1回答:1

我正在使用Java和Html单元解析一个网站。这是我要点击的按钮

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

这是我的代码的一部分

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 !");
    }
}

但是,单击按钮后它仍处于同一页面。我认为原因可能就是这条线

client.getOptions().setJavaScriptEnabled(false);

函数click()无法成功执行,因为我启用了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.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'.

整个程序将在开始时存货。

我该怎么做才能解决这个问题?

java parsing click htmlunit
1个回答
0
投票

我是由Jsoup解决的。

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

我认为这有点像scrapy中的请求。

希望它可能对有困惑的人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.