我try 了所有的选项从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>
我的一些try :
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");
}
}
我已经试着找了两天的解决方案.谢谢你的任何帮助!
EDIT岁的谢碧燕26岁的S发表了 comments ,但我仍然有同样的问题:
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>