私がやろうとしているのは、Web サイトにアクセスし、2 つのドロップダウン リストから項目を選択し、ボックスに何かを入力して、ボタンを押すことです。次に、入力できないボックスに表示される結果のテキストを取得したいと思います。
私が使用したいサイトは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: elementName=[option] attributeName=[value] attributeValue=[English] at com.gargoylesoftware.htmlunit.html.HtmlSelect.getOptionByValue(HtmlSelect.java:437) at maple .Main.main(メイン.java:43)
43行目は
HtmlOption option = select.getOptionByValue("English");