unknown error: failed to write prefs file for loading multiple chrome profile

六月ゝ 毕业季﹏ 提交于 2019-12-13 02:18:00

问题


I have difficult time trying to load multiple chrome profile at the same time, and it always show "unknown error: failed to write prefs file" .I'm using selenium 2.48.2 and Selenium.WebDriver.ChromeDriver 2.20.0.0

    string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
      String chromeLocalAppDataPath = localAppDataPath + @"\Google\Chrome\User Data\Default\";


          var options = new ChromeOptions();
                        options.AddArgument("--no-experiments");
                        options.AddArguments("user-data-dir=" + chromeLocalAppDataPath);
                        options.AddArgument("--disable-translate");
                        options.AddArguments("--start-maximized");
                        //options.AddArgument("--disable-plugins");
                        //options.AddArgument("--disable-extensions");
                        options.AddArgument("--no-default-browser-check");
                        options.AddArgument("--clear-token-service");
                        options.AddArgument("--disable-default-apps");
                        options.AddArgument("--no-displaying-insecure-content");
                        //options.AddArgument("--disable-block-external-urls");
                        options.AddArgument("--disable-bundled-ppapi-flash"); 
            using (abcDataContext db = new abcDataContext())
                        {                    
                                Parallel.ForEach(fixtures, (current) =>
                                {  
                                    using (IWebDriver driver = new ChromeDriver(options))   // fail right here
                                    {
                                        driver.Navigate().GoToUrl("http://google.com");
                                    }
                                });
                                counter++;
                            }
                            while (counter <= fixtures.Count() / 3);
}

UPDATE: using firefoxdriver, it works fine with default profile

FirefoxProfileManager profileManager = new FirefoxProfileManager();
                FirefoxProfile profile = profileManager.GetProfile("default");

Parallel.ForEach(collections, (current) =>
                    {

                        IWebDriver driver = new FirefoxDriver(profile);
                        driver.Navigate().GoToUrl("http://google.com");

                    });

回答1:


Multiple chrome instances cannot use same user-data-dir at same time.

If you want to use same profile, you have to copy the original profile to different temporary location and pass that location as user-data-dir.




回答2:


What is "fixtures"? Is this collection thread-safe?

Also, IWebDriver/ChromeDriver is not thread-safe.

You can't perform this operation in Parallel.Foreach loop. You need to either put lock outside this operation, but then it will not improve any performance which you intend by using parallel.Foreach.




回答3:


According to this link, this is a known issue with newer chrome driver versions. One of the comments made on 21 July says the following:

Exception "unknown error: failed to write prefs file" is thrown when same 
user-data-dir and profile is used by two chrome instances in parallel.
This is reproducible in chromedriver:2.15

I am using chromedriver 2.12.301324 and do not have this problem.




回答4:


It's caused by parallel execution of ChromeDriver. Other errors as "failed to write first run file" or "cannot create default profile directory" may happen.

My solution was to specify option user-data-dir. Two concurrent Chromedriver should not use same user data directory.

chromeOptions.AddArgument("--user-data-dir=C:\\tmp\\chromeprofiles\\profile" + someKindOfIdOrIndex);

You can of course change the path for whatever you want :)




回答5:


It is caused because of space issue in C: Drive.

Clear some space in C Drive so that the browser can create temp folders and profiles.



来源:https://stackoverflow.com/questions/34107651/unknown-error-failed-to-write-prefs-file-for-loading-multiple-chrome-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!