htmlunit 如何监听来自页面的alert 请说的详细些 3C

package com.hommsun.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.util.Assert;

import com.gargoylesoftware.htmlunit.AlertHandler;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class weizai implements AlertHandler {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
webClient.getCookieManager().setCookiesEnabled(true);// 开启cookie管理
webClient.getOptions().setTimeout(90000);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.setJavaScriptTimeout(40000);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);

    HtmlPage page= webClient.getPage("http://192.168.1.108/");

}

@Override
public void handleAlert(Page arg0, String arg1) {

}

}

以上的连接是我些的测试页面 会直接弹出 alert 控制台也会输出

10:35:52.651 [main] WARN com.gargoylesoftware.htmlunit.javascript.host.Window - window.alert("asdsa") no alert handler installed

我想拿到提示 然后 给页面显示

2个回答

WebClient web = new WebClient(BrowserVersion.CHROME);

    web.setIncorrectnessListener(new IncorrectnessListener() {
        @Override
        public void notify(String arg0, Object arg1) {
        }
    });
    web.setCssErrorHandler(new ErrorHandler() {

        @Override
        public void warning(CSSParseException arg0) throws CSSException {
        }

        @Override
        public void fatalError(CSSParseException arg0) throws CSSException {
        }

        @Override
        public void error(CSSParseException arg0) throws CSSException {
        }
    });

    web.setJavaScriptErrorListener(new JavaScriptErrorListener() {

        @Override
        public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
            System.out.println(arg2.getMessage());
            System.out.println(arg0.asXml());
            System.out.println(arg1.getRef());
            try {
                Thread.sleep(3000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void malformedScriptURL(HtmlPage arg0, String arg1,
                MalformedURLException arg2) {
        }

        @Override
        public void scriptException(HtmlPage arg0, ScriptException arg1) {
            System.out.println(arg0.asXml());
            System.out.println(arg1.getMessage());
            try {
                Thread.sleep(3000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
        }
    });

    web.setHTMLParserListener(new HTMLParserListener() {

        @Override
        public void warning(String arg0, URL arg1, String arg2, int arg3,
                int arg4, String arg5) {
            // TODO Auto-generated method stub
        }

        @Override
        public void error(String arg0, URL arg1, String arg2, int arg3,
                int arg4, String arg5) {
            // TODO Auto-generated method stub

        }
    });


    兄台 看懂没有。

这样 更好

webClient.setAlertHandler(new AlertHandler() {

        @Override
        public void handleAlert(Page page, String message) {
            System.out.println(message);

        }
    });
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问