首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Chromedriver:如何使用selenium翻译页面?

Chromedriver:如何使用selenium翻译页面?
EN

Stack Overflow用户
提问于 2018-12-11 04:34:34
回答 2查看 4K关注 0票数 2

我需要翻译一页从日语到英语,在铬浏览器中使用selenium。我尝试了不同的方法,其中一个示例代码片段如下所示

代码语言:javascript
运行
复制
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.addArgumentsoptions.setExperimentalOption,但是没有一个能给我建议什么是解决方案。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-11 15:07:16

您需要启用、翻译并将目标语言ID添加到白名单{"from" : "to"}中。

代码语言:javascript
运行
复制
"translate":{"enabled":"true"}
"translate_whitelists": {"ja":"en"}

在java中:

代码语言:javascript
运行
复制
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);
票数 5
EN

Stack Overflow用户

发布于 2021-07-13 19:46:08

这是一个C#版本,在"AddUserProfilePreference“中直接添加”翻译“和"translate_whitelists”

代码语言:javascript
运行
复制
ChromeOptions options = new ChromeOptions();

Dictionary<string, object> LanguageList = new Dictionary<string, object>();
LanguageList.Add("fr", "en");

Dictionary<string, bool> enableObject = new Dictionary<string, bool>();
enableObj.Add("enabled", true);

options.AddUserProfilePreference("translate", enableObject);
options.AddUserProfilePreference("translate_whitelists", LanguageList);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53717431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档