1

i tried all the options from the stackoverflow site, I'm an intern in programming. Simply indicate how to click.

Site: https://calculator888.ru/random-generator/sluchaynoye-slovo

Button:

<div class="knop_pusk_blok">
   <input type="button" id="genr" data-tip="skivo" value="Случайное Слово" class="knop_dstv_vchl">
</div>

Some of my attempts:


ublic class Main2 {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("start");
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getCookieManager().setCookiesEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.waitForBackgroundJavaScriptStartingBefore(10000);
        webClient.waitForBackgroundJavaScript(10000);


        HtmlPage page = webClient.getPage("https://calculator888.ru/random-generator/sluchaynoye-slovo");


        List<HtmlElement> countOfWords = page.getByXPath("//*[@id=\"klch\"]");
        System.out.println("Count of words is " + countOfWords.size());

        List<HtmlElement> setOfWords = page.getByXPath("//*[@id=\"psz\"]");//word!!1111
        System.out.println("set of words is " + setOfWords.size());

        for (HtmlElement item : countOfWords)
            item.setAttribute("data-vpmn", "10");

        countOfWords = page.getByXPath("//*[@id=\"klch\"]");
        System.out.println("Count of words AFTER is " + countOfWords.get(0).getAttribute("data-vpmn"));

        //press button
        HtmlInput input = page.getHtmlElementById("genr");
        //Html + ButtonInput + Input + SubmitInput

        HtmlPage after = input.click();
        webClient.waitForBackgroundJavaScript(10000);

        for (int i = 0; i < 20; i++) {
            if (after.getByXPath("//*[@id=\"psz\"]").size() != 1) {
                break;
            }
            synchronized (page) {
                page.wait(500);
            }
        }
        

        List<HtmlElement> setOfWords1 = after.getByXPath("//*[@id=\"psz\"]");//after
        System.out.println("set of words is AFTER CLICK " + setOfWords1.size());

        System.out.println("end");
    }

}

I've been trying to find a solution for two days now. Thanks for the any help!

EDIT after Cibiyanna26's comment, but still i had same problem:

package org.example;


import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlElement;

import java.io.IOException;

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

        WebClient webClient = new WebClient();
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setJavaScriptEnabled(false);
        HtmlPage page = webClient.getPage("https://calculator888.ru/random-generator/sluchaynoye-slovo");
        webClient.waitForBackgroundJavaScript(10000);

        HtmlElement htmlInputLineOfWords = (HtmlElement) page.getByXPath("//*[@id=\"bov\"]").get(0);
        System.out.println("before click =" + htmlInputLineOfWords.getTextContent());
        webClient.waitForBackgroundJavaScript(10000);
        HtmlElement button = (HtmlElement) page.getElementById("genr");

        page = button.click();
        htmlInputLineOfWords = (HtmlElement) page.getByXPath("//*[@id=\"bov\"]").get(0);
        System.out.println("after click = " + htmlInputLineOfWords.getTextContent());
            System.out.println(page.asText());

    }
}

and pom.xml:

<dependency>
   <groupId>net.sourceforge.htmlunit</groupId>
   <artifactId>htmlunit</artifactId>
   <version>2.13</version>
</dependency>

1
  • Will have a look.... Commented Dec 21, 2023 at 9:34

2 Answers 2

0

Ok, this page is a bit tricky but with some hacking we can make it working. The code below works here with HtmlUnit version 3.9.0.

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;

import org.htmlunit.BrowserVersion;
import org.htmlunit.WebClient;
import org.htmlunit.WebRequest;
import org.htmlunit.WebResponse;
import org.htmlunit.WebResponseData;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.util.WebConnectionWrapper;

public class Random {

    public static void main(String[] args) throws IOException {
        String url = "https://calculator888.ru/random-generator/sluchaynoye-slovo";

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

            new WebConnectionWrapper(webClient) {

                @Override
                public WebResponse getResponse(final WebRequest request) throws IOException {
                    final URL url = request.getUrl();

                    // block some tracker scripts
                    if ("https://mc.yandex.ru/metrika/tag.js".equals(url.toExternalForm())
                            || "https://yandex.ru/ads/system/context.js".equals(url.toExternalForm())) {
                        // construct empty response
                        final String content = "";
                        final WebResponseData data = new WebResponseData(content.getBytes(StandardCharsets.UTF_8), 200, "blocked", Collections.emptyList());
                        final WebResponse blocked = new WebResponse(data, request, 0L);
                        return blocked;
                    }

                    if (url.toExternalForm().startsWith("https://calculator888.ru/js/generator-sluchaynyh/gener_sluch_chisl.js")) {
                        String js = super.getResponse(request).getContentAsString();

                        // this js contains some statements not supported by our Rhino based js engine
                        // replace them with something working
                        js = js.replace("function pskGen(rrr=(0O57060516-0xbc614d)){",
                                "function pskGen(){"
                                + "var rrr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");
                        js = js.replace("function gSch(rrr=(0O57060516-0xbc614d)){",
                                "function gSch(){"
                                + "var rrr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");
                        js = js.replace("function itogPkazDann(shr=(0O57060516-0xbc614d)){",
                                "function itogPkazDann(){"
                                + "var shr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");
                        js = js.replace("function gBkr(zmn=(0x75bcd15-0O726746425)){",
                                "function gBkr(){"
                                + "var zmn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0x75bcd15-0O726746425;");
                        js = js.replace("function gDnr(rrr=(0O57060516-0xbc614d)){",
                                "function gDnr(){"
                                + "var rrr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");
                        js = js.replace("function itogPkazDann(rrr=(0O57060516-0xbc614d)){",
                                "function itogPkazDann(){"
                                + "var rrr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");
                        js = js.replace("function pkazDannItogNik(rrr=(0O57060516-0xbc614d)){",
                                "function pkazDannItogNik(){"
                                + "var rrr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");
                        js = js.replace("function pkazDannItog(rrr=(0O57060516-0xbc614d)){",
                                "function pkazDannItog(){"
                                + "var rrr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 12345678 - 0xbc614d;");

                        js = js.replace("function rbotPkazItog(okn_msv,sohr=(0O57060516-0xbc614d)){",
                                "function rbotPkazItog(okn_msv){"
                                + "var sohr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 12345678 - 0xbc614d;");
                        js = js.replace("function mySortVse(dan_mas,kak=(0x75bcd15-0O726746425)){",
                                "function mySortVse(dan_mas){"
                                + "var kak = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0x75bcd15 - 0O726746425;");

                        // not correct but seems to have no effect for this problem
                        js = js.replace("return {[cYdk]:rzd_hls,[wl6j]:rzd_okn};",
                                "return 'todo';");

                        // construct alternative response
                        final WebResponseData patchedJs = new WebResponseData(js.getBytes(StandardCharsets.UTF_8), 200, "OK", Collections.emptyList());
                        return new WebResponse(patchedJs, request, 0L);
                    }

                    return super.getResponse(request);
                }
            };

            HtmlPage page = webClient.getPage(url);

            String result = page.getElementById("bov").asNormalizedText();
            System.out.println(result);

            page.getElementById("genr").click();
            webClient.waitForBackgroundJavaScriptStartingBefore(1_000);

            result = page.getElementById("bov").asNormalizedText();
            System.out.println(result);

            page.getElementById("genr").click();
            webClient.waitForBackgroundJavaScriptStartingBefore(1_000);

            result = page.getElementById("bov").asNormalizedText();
            System.out.println(result);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

it's working; but for me it’s very difficult, for you it’s not, I wanted to learn and not torment a person. such a difficult decision, I think I chose the wrong approach. thank you for your time, you are my hero
0

Check your dependencies whether they are correctly installed or not

then try out this :

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlUnitExample {
    public static void main(String[] args) {
        try (final WebClient webClient = new WebClient()) {
            webClient.getOptions().setJavaScriptEnabled(false);
            final HtmlPage page = webClient.getPage("https://example.com");

            
            final HtmlButton button = page.getElementById("buttonId");
            button.click();
            System.out.println(page.asText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.