htmlunit - 在激活JavaScript函数后获取新页面

时间:2017-11-28 14:33:06

标签: javascript htmlunit

我有一个WebClient的HtmlPage。这个页面(HtmlPage)有一个按钮,我想点击那个按钮来获得一个新页面(另一个页面)。但是当我单击该按钮时,结果返回与原始页面相同的页面。以下是我的代码:

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import java.net.URL;

public class GetLink2 {
public static void main(String[] args) throws Exception {
    URL url = new URL("https://fptshop.com.vn/");

    String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0";
    WebRequest request = new WebRequest(url);
    request.setAdditionalHeader("User-Agent", userAgent);
    //request.setAdditionalHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);

    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.setJavaScriptTimeout(20000);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setUseInsecureSSL(true);

    webClient.waitForBackgroundJavaScript(5000);

    HtmlPage page = webClient.getPage(request);

    for (DomElement input : page.getElementsByTagName("input")) {
        if (input.getAttribute("placeholder").contains("tìm") ||
                input.getAttribute("placeholder").contains("Tìm")) {
            System.out.println(input.asXml());
            System.out.println("Set element focused: " + page.setFocusedElement(input));

            input.setAttribute("id", "my_input_search");
            System.out.println("Element focused: " + page.getFocusedElement());

            //pageResult = page.pressAccessKey((char) DOM_VK_RETURN).getHtmlPageOrNull().getUrl();
            //System.out.println("Result: " + pageResult.toString());
            //System.out.printf("Result: %s%n", input.fireEvent(String.valueOf(KeyboardEvent.DOM_VK_RETURN)).getJavaScriptResult().toString());
            break;
        }

        //System.out.println(htmlElement.asXml());

        //result.getNewPage();
        //System.out.println("result: "+ result.getNewPage().getUrl());
    }


    System.out.println("Input search: " + page.getElementById("my_input_search"));

    String jsScript = "var element = document.getElementById('my_input_search');" +
            "element.value = 'iphone 7';" +
            "element.addEventListener('keypress', function (e) {" +
            "console.log(e.key, e.char, e.keyCode)});" +
            "var e = new KeyboardEvent('keypress', {" +
            "bubbles: true, cancelable: true, char: 'Enter',key: 'enter', keyCode: 13});" +
            "element.dispatchEvent(e);";

    System.out.println("Input Search After Set Value: " + page.getElementById("my_input_search"));
    page.executeJavaScript(jsScript);
    Thread.sleep(20000);
    page.getPage();
    System.out.println("Result " + page.getPage());
}
}

请大家帮助我,谢谢你阅读我的问题。

1 个答案:

答案 0 :(得分:0)

你正在失去你的结果,因为

page.executeJavaScript(jsScript);

返回ScriptResult,您应该

Page newPage = page.executeJavaScript(jsScript).getNewPage();
System.out.println("Result " + newPage);