微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用htmlunit直接从Google翻译翻译文本?

如何解决如何使用htmlunit直接从Google翻译翻译文本?

我需要使用htmlunit来使用Google翻译器将文本从英语翻译为西班牙语。 这是我的代码...(我是htmlunit的新手)。

    public static void main(String[] args){
    try(WebClient webClient = new WebClient(browserVersion.CHROME)){
    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
    System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
    webClient.getoptions().setJavaScriptEnabled(true);
    webClient.getoptions().setCssEnabled(false);
    webClient.getoptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());

    String link = "https://www.google.com/search?q=Traductor&oq=tra&aqs=chrome.0.69i59l2j69i57j69i59j0j69i61j69i60l2.921j0j7&sourceid=chrome&ie=UTF-8";
    HtmlPage page = webClient.getPage(link);
    webClient.waitForBackgroundJavaScript(15000);
    final HtmlTextArea textarea = (HtmlTextArea) page.getElementById("tw-source-text-ta");
    final DomElement translation = page.getElementById("tw-target-text");
    textarea.type("This is a test");
    webClient.waitForBackgroundJavaScript(10000);
    System.out.println(textarea.getTextContent());
    System.out.println("The translated text is: " + translation.getTextContent());
    
    }catch(Exception e){
        System.out.println(e);
    }
}

在控制台中打印时,我看到textarea编写正确,但是没有返回翻译后的文本。我认为Google翻译不希望Ajax,即使它已经放置了它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。