简体   繁体   中英

htmlunit - Get new page after excuting a JavaScript function

I have an HtmlPage by WebClient. This page (HtmlPage) has a button, I want to click that button to get a new page (another page). But when I clicked that button, the result returned the same page with the original page. Below is my code:

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());
}
}

Can everyone help me please, thanks for reading my question.

You are loosing your result, because

page.executeJavaScript(jsScript);

returns a ScriptResult , you should do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM