HtmlUnit / htmlunit

HtmlUnit is a "GUI-Less browser for Java programs".
https://www.htmlunit.org
Apache License 2.0
873 stars 172 forks source link

HTMLUnit not calling the JQuery on key release events #644

Closed gangadharkasturi closed 1 year ago

gangadharkasturi commented 1 year ago

HTMLUnit not calling the JQuery on key release events.

Sample Code:

InterceptWebConnection interceptWebConnection = new InterceptWebConnection(webClient);
HtmlPage page = webClient.getPage("https://www.mca.gov.in/content/mca/global/en/mca/master-data/MDS.html");
HtmlTextInput elementById = (HtmlTextInput) page.getElementById("masterdata-search-box");
page.getWebClient().waitForBackgroundJavaScriptStartingBefore(20000);
elementById.setValueAttribute("TATA");
HtmlTable table = (HtmlTable) page
    .getByXPath("//*[@id=\"fohomepage-037f88dfd8\"]/div/div/div[3]/div/div[1]/div[1]/div/div[2]/table")
    .get(0);

String str = "";
HtmlTableBody htmlTableBody = table.getBodies().get(0);
final List<HtmlTableRow> rows = htmlTableBody.getRows();
for (final HtmlTableCell cell : rows.get(0).getCells()) {
    str = str + cell.asNormalizedText() + "\n";
}
System.err.println(str);

Support class;

class InterceptWebConnection extends FalsifyingWebConnection {
    public InterceptWebConnection(WebClient webClient) throws IllegalArgumentException {
        super(webClient);
    }

    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        WebResponse response = super.getResponse(request);
        if (response.getWebRequest().getUrl().toString().endsWith("disable-devtool")) {
            return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
        }
        return super.getResponse(request);
    }
}

We are supposed to receive the suggestion but that is not happening

rbri commented 1 year ago

@gangadharkasturi

Have done some small changes

Finally the code below produces:

TATA 1MG HEALTHCARE SOLUTIONS PRIVATE LIMITED - U24290DL2016PTC302634 - Delhi - Active -  - 
TATA 1MG TECHNOLOGIES PRIVATE LIMITED - U74140DL2015PTC279229 - Delhi - Active -  - 
TATA ADVANCED MATERIALS LIMITED - U85110KA1989PLC013224 - Karnataka - Amalgamated -  - 
TATA ADVANCED SYSTEMS LIMITED - U72900TG2006PLC077939 - Telangana - Active -  - 
TATA AIA LIFE INSURANCE COMPANY LIMITED - U66010MH2000PLC128403 - Maharashtra - Active -  - 
TATA AIG GENERAL INSURANCE COMPANY LIMITED - U85110MH2000PLC128425 - Maharashtra - Active -  - 
TATA ALUMINIUM LIMITED - U14200DL1991PLC042748 - Delhi - Strike Off -  - 
TATA ASSET MANAGEMENT PRIVATE LIMITED - U65990MH1994PTC077090 - Maharashtra - Active -  - 
TATAATSU IDEALABS PRIVATE LIMITED - U72900KA2011PTC060663 - Karnataka - Active -  - 
TATA AUTOCOMP GOTION GREEN ENERGY SOLUTIONS PRIVATE LIMITED - U29304PN2020PTC190510 - Maharashtra - Active -  - 

My code:

public class Test {

    private static final String url = "https://www.mca.gov.in/content/mca/global/en/mca/master-data/MDS.html";

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

        try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
            webClient.getOptions().setThrowExceptionOnScriptError(false);

            new InterceptWebConnection(webClient);

            HtmlPage page = webClient.getPage(url);
            webClient.waitForBackgroundJavaScriptStartingBefore(10_000);

            HtmlTextInput elementById = (HtmlTextInput) page.getElementById("masterdata-search-box");
            elementById.type("TATA");

            webClient.waitForBackgroundJavaScriptStartingBefore(2_000);

            HtmlTable table = (HtmlTable) page
                .getByXPath("//*[@id=\"fohomepage-037f88dfd8\"]/div/div/div[3]/div/div[1]/div[1]/div/div[2]/table")
                .get(0);

            StringBuffer str = new StringBuffer();
            HtmlTableBody htmlTableBody = table.getBodies().get(0);
            for (final HtmlTableRow row : htmlTableBody.getRows()) {
                for (final HtmlTableCell cell : row.getCells()) {
                    str.append(cell.asNormalizedText()).append(" - ");
                }
                str.append("\n");
            }

            System.out.println(str);
        }
    }

    private static final class InterceptWebConnection extends FalsifyingWebConnection {
        public InterceptWebConnection(WebClient webClient) throws IllegalArgumentException {
            super(webClient);
        }

        @Override
        public WebResponse getResponse(WebRequest request) throws IOException {
            WebResponse response = super.getResponse(request);
            String respUrl = response.getWebRequest().getUrl().toString();
            if (respUrl.endsWith("disable-devtool") || respUrl.endsWith("clientlib-site.min.js")) {
                return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");
            }
            return super.getResponse(request);
        }
    }
}

Hope that helps

gangadharkasturi commented 1 year ago

It works, Thanks for the help.

rbri commented 1 year ago

Great (github stars are always welcome - and if you use HtmlUnit for business purposes you can also think about sponsoring)