使用 htmlunit 和 java 单击按钮

问题描述 投票:0回答:1

我尝试了 stackoverflow 网站上的所有选项,我是一名编程实习生。只需指示如何点击即可。

网站:https://calculator888.ru/random-generator/sluchaynoye-slovo

按钮:

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

我的一些尝试:


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

}

我这两天一直在寻找解决方案。感谢您的帮助!

Cibiyanna26 评论后

编辑

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

    }
}

和pom.xml:

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

java html ajax xml htmlunit
1个回答
0
投票

检查您的依赖项是否正确安装

然后试试这个:

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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.