PhantomJS C#如何更改默认语言

时间:2016-01-26 23:04:46

标签: c# selenium-webdriver phantomjs

尝试在我的程序中设置默认语言。但经过大量的谷歌搜索,我发现任何答案。

我如何输入标准的英语接受语言? 我可以阅读任何文件,我可以使用PhantomJS在c#中使用的所有功能吗?我dident找到了更多,然后在Visualstudio控件中播放。

var driverService = PhantomJSDriverService.CreateDefaultService();

driverService.HideCommandPromptWindow = true;

driverService.LoadImages = false;
driverService.SslProtocol = "tlsv1";
driverService.IgnoreSslErrors = true;
driverService.ProxyType = "http";
driverService.Proxy = "";

using (var driver = new PhantomJSDriver(driverService))
{
    driver.Manage().Window.Size = new Size(1920, 1080); // Size is a type in assembly "System.Drawing"
    driver.Manage().Cookies.DeleteAllCookies();

    driver.Url = "https://www.thewebsite.com";

    Thread.Sleep(5000); // 5sec

    try
    {
        driver.FindElement(By.Name("email")).SendKeys("MyEmail");
        driver.FindElement(By.Name("fullName")).SendKeys("MyName");
        driver.FindElement(By.Name("username")).SendKeys("MyUsername");
        driver.FindElement(By.Name("password")).SendKeys("MyPassword");

        driver.TakeScreenshot().SaveAsFile("LetsSnapascreenshot.png", ImageFormat.Png);

    }
    catch (OpenQA.Selenium.NoSuchElementException exception)
    {
        var exmsg = exception;
        driver.TakeScreenshot().SaveAsFile("Snaperrorscreenshot.png", ImageFormat.Png);
    }
}

2 个答案:

答案 0 :(得分:1)

如果其他人正在寻找它,我找到了解决方案。

PhantomJSOptions options = new PhantomJSOptions();

options.AddAdditionalCapability("phantomjs.page.customHeaders.Accept-Language", "en,en;q=0.5");
IWebDriver driver = new PhantomJSDriver(options);

现在它输出了接受语言正确的

答案 1 :(得分:0)

您可以尝试添加标题

PhantomJSOptions options = new PhantomJSOptions();
options.AddAdditionalCapability("Accept-Language","en-DE,en;q=0.5"); 
IWebDriver driver = new PhantomJSDriver(options);

或按照in this answer

所述写入index.html
<script>
   localStorage.lang = 'en';
</script>