Chromedriver:如何使用 selenium 翻译页面? [英] Chromedriver: How to translate a page using selenium?

查看:80
本文介绍了Chromedriver:如何使用 selenium 翻译页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 chrome 浏览器中使用 selenium 将页面从日语翻译成英语.我尝试了不同的方式,示例代码片段之一如下

I need to translate a page from Japanese to English, using selenium in chrome browser. I tried different ways one of sample code snippet is as following

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Main {

    private WebDriver driver=null;
    WebDriverLoad a;

    @Test
    public void successfulDesignerLogin() throws Exception{
//      final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//        capabilities.setJavascriptEnabled(true);
        String chromedriver =  "/dev/Saved/chromedriver";
        System.setProperty("webdriver.chrome.driver",chromedriver);

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--lang=en-ca");
        //Map<String, Object> prefs = new HashMap<String, Object>();
        //prefs.put("intl.accept_languages", "en,en_US");
        //options.setExperimentalOption("prefs", prefs);


       ChromeDriver driver = new ChromeDriver(options);
       driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
       driver.get("https://www.bbc.com/japanese");
       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
       driver.close();


}
}

我尝试了几个解决方案 options.addArguments options.setExperimentalOption 但没有任何效果,任何人都可以建议我解决方案

I tried a couple of solution options.addArguments options.setExperimentalOption but nothing works can any one suggests me what can be the solution

推荐答案

您需要开启翻译功能并将目标语言ID加入白名单{"from" : "to"}.

You need to enable translate and add target language ID to the whitelist {"from" : "to"}.

"translate":{"enabled":"true"}
"translate_whitelists": {"ja":"en"}

在java中:

Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> langs = new HashMap<String, Object>();
langs.put("ja", "en");
prefs.put("translate", "{'enabled' : true}");
prefs.put("translate_whitelists", langs);
options.setExperimentalOption("prefs", prefs);

这篇关于Chromedriver:如何使用 selenium 翻译页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆