尝试在名为TimeEdit的页面上使用HTMLUnit,该页面主要用于安排学校时间表。我想在此处的输入字段中添加搜索词(“ DV1431”):
TimeEdit
然后我想以某种方式提交。我已经读过您可以使用HTMLUnit触发JavaScript,还可以使用按钮甚至创建“假按钮”。但是我不确定哪种情况最好。
我已经尝试过在Stackoverflow上找到的解决方案:Youtube-example
这是可行的,但是在我的页面TimeEdit上没有可以使用的HTML表单。相反,只有一个名为searchButtons的类,其中位于commit-button和input字段。
这是我的示例代码:
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getCookieManager().setCookiesEnabled(true);
//Get the page
HtmlPage currentPage = webClient.getPage("https://se.timeedit.net/web/bth/db1/sched1/ri1Q7.html");
//Just for testing purpose
System.out.println(currentPage.getUrl());
// Get form where submit button is located
// But on this page there is no form, just a class called searchButtons
HtmlForm searchForm = (HtmlForm) currentPage.getElementById("ffsearchname");
// Get the input field.
HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("ffsearchname");
// Insert the search term.
searchInput.setText("DV1431");
// Workaround: create a 'fake' button and add it to the form.
HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
submitButton.setAttribute("type", "submit");
searchForm.appendChild(submitButton);
// Workaround: use the reference to the button to submit the form.
HtmlPage newPage = submitButton.click();
//Testing purpose
System.out.println(newPage.getUrl());
如果不在表单中,如何访问提交按钮和输入字段?如何在输入字段中设置搜索词,如何提交?
最佳答案
不知道为什么您尝试插入按钮以及为什么期望该按钮可能有用。
如果您想自动化一些HTML页面,则需要对html的方式有一些基本的了解,在您的情况下还需要javascript的工作方式。
// no need to set any options just use the default
WebClient webClient = new WebClient(BrowserVersion.CHROME);
// Get the page and wait for the javacode that will start with an minimal delay
// doing something like setTimeout(init(), 10); is a common trick done by some js libs
HtmlPage currentPage = webClient.getPage("https://se.timeedit.net/web/bth/db1/sched1/ri1Q7.html");
currentPage.getEnclosingWindow().getJobManager().waitForJobsStartingBefore(100);
// Get the input field.
HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("ffsearchname");
// Insert the search term.
searchInput.setText("DV1431");
// the output
DomElement output = currentPage.getElementById("objectsearchresult");
System.out.println("- before -------------------------------------------------------------------");
System.out.println(output.asText());
System.out.println("----------------------------------------------------------------------------");
// try to find the button
for (final DomElement elem : currentPage.getElementsByTagName("input")) {
if ("Sök".equals(((HtmlInput) elem).getValueAttribute())) {
// click and again wait for the javascript
currentPage = elem.click();
currentPage.getEnclosingWindow().getJobManager().waitForJobsStartingBefore(100);
System.out.println();
output = currentPage.getElementById("objectsearchresult");
System.out.println("- after --------------------------------------------------------------------");
System.out.println(output.asText());
System.out.println("----------------------------------------------------------------------------");
break;
}
}
顺便说一句:此代码仅是证明HtmlUnit能够自动执行页面的示例。为了找到控件,您应该研究api提供的各种选项,并为您的用例选择合适的选项(也许是XPath)。
关于javascript - HTMLUnit HtmlTextInput和SubmitButton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41462478/