我想做的是去一个网站,从两个下拉列表中选择项目,在一个框中输入一些东西,然后按下一个按钮。然后,我想获得将在您无法输入的框中的结果文本。
我想使用的网站是http://www.worldlingo.com/en/products_services/worldlingo_translator.html,这是我的代码,除了获取最终文本之外,我正在尝试做所有事情(因为我不知道如何做到这一点)。
try {
final WebClient webClient = new WebClient();
Page page = webClient.getPage("http://www.worldlingo.com/en/products_services/worldlingo_translator.html");
HtmlSelect select = ((HtmlSelect) page).getElementById("SelSrcLang");
HtmlOption option = select.getOptionByValue("English");
select.setSelectedAttribute(option, true);
HtmlSelect select2 = ((HtmlSelect) page).getElementById("SelTrgLang");
HtmlOption option2 = select2.getOptionByValue("French");
select.setSelectedAttribute(option2, true);
final HtmlForm form = ((HtmlPage) page).getFormByName("form");
final HtmlSubmitInput button = form.getInputByName("Translate");
final HtmlTextInput textField = form.getInputByName("wl_ft_print_text");
textField.setValueAttribute("How are you?");
page = (HtmlPage) form.getInputByValue("Translate").click();
System.out.println("Finished");
} catch (FailingHttpStatusCodeException e) { } catch (MalformedURLException e) { } catch (IOException e) { }
编辑:更改后
Page page = webCLient...
至
HtmlPage page = webCLient...
我没有收到任何错误,但是当我运行时出现以下错误
线程“主”com.gargoylesoftware.htmlunit.ElementNotFoundException 中的异常:在枫树的 com.gargoylesoftware.htmlunit.html.HtmlSelect.getOptionByValue(HtmlSelect.java:437) 处的 elementName=[option] attributeName=[value] attributeValue=[English] .Main.main(Main.java:43)
第 43 行是
HtmlOption option = select.getOptionByValue("English");