为了账号安全,请及时绑定邮箱和手机立即绑定

HtmlUnit编码

HtmlUnit编码

守着一只汪 2023-11-01 22:32:14
每当我单击按钮时,我都会得到错误的编码。如何修复它?    package com.company;    import com.gargoylesoftware.htmlunit.*;    import com.gargoylesoftware.htmlunit.html.HtmlElement;    import com.gargoylesoftware.htmlunit.html.HtmlPage;    import org.apache.commons.logging.LogFactory;          import java.util.logging.Level;    public class Parsing {        public void connect() throws Exception {            LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");            java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);            java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);            WebClient client = new WebClient(BrowserVersion.CHROME);            client.setAjaxController(new NicelyResynchronizingAjaxController());            client.getOptions().setThrowExceptionOnScriptError(false);            client.getOptions().setThrowExceptionOnFailingStatusCode(false);            String url = "https://auto.ru/cars/new/group/mercedes/e_klasse/20743577/20886691/?grouping_id=tech_param_id%3D20743577%2Ccomplectation_id%3D21225392&sort=fresh_relevance_1-desc&geo_radius=200";            HtmlPage page = client.getPage(url);            HtmlElement clickContact = (HtmlElement) page.getElementById("confirm-button");            page = clickContact.click();            client.waitForBackgroundJavaScript(10000);            System.out.println(page.asText());        }输出:唷唷唷!...
查看完整描述

1 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

尝试这个


public static void main(String[] args) throws IOException {

    String url = "https://auto.ru/cars/new/group/mercedes/e_klasse/20743577/20886691/?grouping_id=tech_param_id%3D20743577%2Ccomplectation_id%3D21225392&sort=fresh_relevance_1-desc&geo_radius=200";


    try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {

        webClient.getOptions().setThrowExceptionOnScriptError(false);


        HtmlPage page = webClient.getPage(url);


        HtmlElement clickContact = (HtmlElement) page.getElementById("confirm-button");

        page = clickContact.click();

        System.out.println("still running " + webClient.waitForBackgroundJavaScript(60_000));

        System.out.println(" ---- ");


        // the page content got changed via javascript

        // we like to get see the new content

        page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();

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

    }

}

我没有得到全部内容,但得到了主要部分。


查看完整回答
反对 回复 2023-11-01
  • 1 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信