如何使用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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...