<i id='lwMYi'><tr id='lwMYi'><dt id='lwMYi'><q id='lwMYi'><span id='lwMYi'><b id='lwMYi'><form id='lwMYi'><ins id='lwMYi'></ins><ul id='lwMYi'></ul><sub id='lwMYi'></sub></form><legend id='lwMYi'></legend><bdo id='lwMYi'><pre id='lwMYi'><center id='lwMYi'></center></pre></bdo></b><th id='lwMYi'></th></span></q></dt></tr></i><div id='lwMYi'><tfoot id='lwMYi'></tfoot><dl id='lwMYi'><fieldset id='lwMYi'></fieldset></dl></div>

    1. <legend id='lwMYi'><style id='lwMYi'><dir id='lwMYi'><q id='lwMYi'></q></dir></style></legend>
        • <bdo id='lwMYi'></bdo><ul id='lwMYi'></ul>

        <small id='lwMYi'></small><noframes id='lwMYi'>

        <tfoot id='lwMYi'></tfoot>

        对不同的 ChromeDriver 实例使用相同的 chrome 配置文件(会话)

        时间:2023-09-14

          <i id='wDF9Q'><tr id='wDF9Q'><dt id='wDF9Q'><q id='wDF9Q'><span id='wDF9Q'><b id='wDF9Q'><form id='wDF9Q'><ins id='wDF9Q'></ins><ul id='wDF9Q'></ul><sub id='wDF9Q'></sub></form><legend id='wDF9Q'></legend><bdo id='wDF9Q'><pre id='wDF9Q'><center id='wDF9Q'></center></pre></bdo></b><th id='wDF9Q'></th></span></q></dt></tr></i><div id='wDF9Q'><tfoot id='wDF9Q'></tfoot><dl id='wDF9Q'><fieldset id='wDF9Q'></fieldset></dl></div>

          1. <legend id='wDF9Q'><style id='wDF9Q'><dir id='wDF9Q'><q id='wDF9Q'></q></dir></style></legend>
            <tfoot id='wDF9Q'></tfoot>

                <tbody id='wDF9Q'></tbody>

              <small id='wDF9Q'></small><noframes id='wDF9Q'>

                • <bdo id='wDF9Q'></bdo><ul id='wDF9Q'></ul>
                  本文介绍了对不同的 ChromeDriver 实例使用相同的 chrome 配置文件(会话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试并行打开多个浏览器,但我无法在多个窗口中导航到该网站..

                  I am trying to open multiple browsers in parallel, but I can not navigate to the website in more than one window..

                  这是我的做法:

                  namespace XXX
                  {
                  public class CoreDriver
                  {
                      public IWebDriver driver;
                      public int my_port { get; set; }
                  
                      public void Initialize()
                      {
                          string chromeee = "";
                          if (my_port == 50147) { chromeee = "C:/Users/AA/Downloads/chromedriver1/"; }
                          else if (my_port == 50148) {chromeee = "C:/Users/AA/Downloads/chromedriver2/"; }
                          else if (my_port == 50149) { chromeee = "C:/Users/AA/Downloads/chromedriver3/"; }
                          else if (my_port == 50140) { chromeee = "C:/Users/AA/Downloads/chromedriver4/"; }
                  
                          ChromeOptions options = new ChromeOptions();
                          options.AddArgument("user-data-dir=C:\Users\AA\AppData\Local\Google\Chrome\User Data");
                  
                          var driverService = ChromeDriverService.CreateDefaultService(chromeee);
                          driverService.HideCommandPromptWindow = true;
                          driverService.Port = my_port;
                  
                          driver = new ChromeDriver(driverService, options);
                          driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0,0,12));
                          driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(13));
                          //driver navigate
                      }
                  }
                  }
                  

                  这样称呼它:

                  CoreDriver A1 = new CoreDriver();
                  A1.my_port = 50147;
                  A1.Initialize();
                  
                  CoreDriver A2 = new CoreDriver();
                  A2.my_port = 50148;
                  A2.Initialize(); // timeout error here
                  
                  // ...
                  

                  不幸的是,打开第二个窗口后 - 显示超时错误:

                  Unfortunately, after the second window is opened - timeout error is shownn:

                  OpenQA.Selenium.WebDriverException"类型的第一次机会异常发生在 WebDriver.dll 中

                  A first chance exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll

                  附加信息:对远程 WebDriver 的 HTTP 请求URL http:/loca1host:50148/session 的服务器在 60 后超时秒.

                  Additional information: The HTTP request to the remote WebDriver server for URL http:/loca1host:50148/session timed out after 60 seconds.

                  在这一行:

                  driver = new ChromeDriver(driverService, options);

                  driver = new ChromeDriver(driverService, options);

                  使用不同的参数重新运行测试后,我发现由于指定的 Chrome 配置文件而显示错误:

                  after rerunning the test with different parameters I have found out that the error is shown due to the specified Chrome profile:

                  options.AddArgument("user-data-dir=C:\Users\AA\AppData\Local\Google\Chrome\User
                  Data");
                  

                  如果我删除该行 - 那么我的所有 cookie 都不会在 ChromeDriver 实例中使用,这不是我可以忍受的:)有没有办法在多个 chromedriver 实例中使用相同的 chrome 配置文件?

                  If I remove the line - then all of my cookies will not be used in ChromeDriver instance and that is not something that I can live with :) Is there a way to use the same chrome profile in multiple chromedriver instances?

                  推荐答案

                  好的,所以我正在使用上述方法.

                  Okay, so I am using my approach as stated above.

                  我的要求是:

                  • 我必须保留主要 chrome 配置文件的 cookie
                  • 我必须保留主要配置文件的扩展名
                  • 我不需要主配置文件的历史记录、打开的选项卡、会话等
                  • 在重新启动现有自定义配置文件后 - 我在没有打开标签的情况下清楚地启动它

                  这是几句话的逻辑.

                  • 首先我为现有的 Google Chrome 配置文件指定一个目录.
                  • 如果我们需要创建 cookie(即登录某个网站),那么我们会在 google chrome 的主配置文件中进行.
                  • 完成后,关闭 chrome.有些网站会保留 cookie 很长时间,有些则不会.因此,在必要时重新登录主配置文件符合我们的利益.不要让原始铬打开!否则 ChromeDriver 会抛出一些警告.
                  • 接下来,我的脚本会将必要的文件夹和文件复制到新文件夹中.此文件夹是我们包含所有 cookie 的新配置文件.在我的 PC 上,所有内容的大小约为 30 兆字节.
                  • 如果新配置文件的文件夹已经存在 - 那么程序将只复制 cookie 文件.这不应超过 1-2 兆 的数据.
                  • First I specify a directory for the existing Google Chrome profile.
                  • If we need to create cookies (i.e. login into some website) then we do it on the main profile of google chrome.
                  • After it is done, close the chrome. Some websites keep cookies for a long time, some - not. So it is in our interest to relogin on the main profile when necessary. Do not keep the Original chrome opened! Otherwise ChromeDriver will throw some warnings.
                  • Next, my script will copy the necessary folders and files into new folder. This folder is our new profile with all cookies. Everything is about 30 megabytes in size on my PC.
                  • If the folder for the new profile already exists - then the program will only copy cookies files. That's shouldn't be more than 1-2 megs of data.

                  这是代码.您可能想要调整一件事或另一件事.

                  And here is the code. You might want to tweak one thing or another.

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Data;
                  using System.Drawing;
                  using System.Linq;
                  using System.Text;
                  using System.Windows.Forms;
                  using OpenQA.Selenium;
                  using OpenQA.Selenium.Chrome;
                  using OpenQA.Selenium.Firefox;
                  using OpenQA.Selenium.Support.UI;
                  using OpenQA.Selenium.Internal;
                  using OpenQA.Selenium.Remote;
                  using System.IO;
                  using System.Drawing.Imaging;
                  using System.Management;
                  using System.Text.RegularExpressions;
                  using System.Threading;
                  using System.Diagnostics;
                  using System.Reflection;
                  using System.Threading.Tasks;
                  using System.Collections.Concurrent;
                  using System.Runtime.InteropServices;
                  using System.Net;
                  
                  namespace NAMESPACE
                  {
                      public class CoreDriver
                      {
                          public IWebDriver driver;
                          public string my_name { get; set; }
                          public int my_port { get; set; }
                  
                          public string default_profile_dir = @"C:UsersUSERNAMEAppDataLocalGoogleChrome";
                          public string chromedriver_path = @"C:UsersUSERNAMEDownloadschromedriver_win32";
                          public string site_profile_path;
                          public string site_profile_path_s;
                          public string default_path;
                  
                          public void Initialize()
                          {
                              ChromeOptions options = new ChromeOptions();
                              options.AddArgument("--log-level=3");
                              options.AddArgument("--test-type");
                              options.AddArgument("--silent");
                              options.AddArgument("user-data-dir=" + site_profile_path_s);
                              options.AddArgument("--disable-plugins"); // disable flash
                  
                              var driverService = ChromeDriverService.CreateDefaultService(chromedriver_path);
                              driverService.HideCommandPromptWindow = true;
                              driverService.Port = my_port;
                  
                              driver = new ChromeDriver(driverService, options);
                              driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 14));
                              driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(15));
                  
                              IJavaScriptExecutor jscript = driver as IJavaScriptExecutor;
                              jscript.ExecuteScript("return window.stop");
                          }
                  
                          public void ConfigureProfile()
                          {
                              site_profile_path_s = default_profile_dir + "profile " + my_name;
                              site_profile_path = site_profile_path_s + @"Default";
                  
                              default_path = default_profile_dir + @"User DataDefault";
                  
                              if (!Directory.Exists(site_profile_path))
                              {
                                  CreateBlankProfile();
                              }
                              else
                              {
                                  // copy existing chrome profile. Keep cache, extensions, etc.
                                  CopyProfileFiles();
                  
                                  // but stay away from opened tabs
                                  RemoveOpenedTabsFiles();
                              }
                  
                          }
                  
                          public void CleanUpOldProfiles()
                          {
                              DirectoryInfo di = new DirectoryInfo(default_profile_dir);
                              DirectoryInfo[] directories = di.GetDirectories("profile*", SearchOption.TopDirectoryOnly);
                              if (directories.Count() > 0)
                              {
                                  foreach (var folder in directories)
                                  {
                                      try
                                      {
                                          Directory.Delete(folder.FullName, true);
                                      }
                                      catch
                                      {
                  
                                      }
                  
                                  }
                  
                              }
                          }
                  
                          public void CreateBlankProfile()
                          {
                              // new profile direftory
                              CreateIfMissing();
                  
                              // copy existing chrome profile. Keep cache, extensions, etc.
                              // but stay away from opened tabs
                              CopyProfileFiles();
                              CopyProfileFolders();
                          }
                  
                          public void CopyProfileFiles()
                          {
                              // default profile location
                              DirectoryInfo di = new DirectoryInfo(default_path);
                  
                              // copy files
                              List<string> file_lib = new List<string>() { "Cookies", "Login", "Preferences", "Secur" };
                              FileInfo[] files = di.GetFiles("*", SearchOption.TopDirectoryOnly);
                              if (files.Count() > 0)
                              {
                                  foreach (var file in files)
                                  {
                                      if (PassFileOrFolder(file.Name, file_lib))
                                      {
                                          file.CopyTo(site_profile_path + @"" + file.Name, true);
                                      }
                  
                                  }
                  
                              }
                          }
                  
                          public void RemoveOpenedTabsFiles()
                          {
                              // default profile location
                              DirectoryInfo di = new DirectoryInfo(site_profile_path);
                  
                              // copy files
                              List<string> file_lib = new List<string>() { "Current", "Last" };
                              FileInfo[] files = di.GetFiles("*", SearchOption.TopDirectoryOnly);
                              if (files.Count() > 0)
                              {
                                  foreach (var file in files)
                                  {
                                      if (PassFileOrFolder(file.Name, file_lib))
                                      {
                                          File.Delete(file.FullName);
                                      }
                  
                                  }
                  
                              }
                          }
                  
                          public void CopyProfileFolders()
                          {
                              // default profile location
                              DirectoryInfo di = new DirectoryInfo(default_path);
                  
                              // copy folders
                              List<string> folder_lib = new List<string>() { "databases", "Extension", " Storage", "Web Applications", "File System", "IndexedDB" };
                              DirectoryInfo[] directories = di.GetDirectories("*", SearchOption.TopDirectoryOnly);
                              if (directories.Count() > 0)
                              {
                                  foreach (var folder in directories)
                                  {
                                      if (PassFileOrFolder(folder.Name, folder_lib))
                                      {
                                          DirectoryCopy(folder.FullName, site_profile_path + @"" + folder.Name, true);
                                      }
                  
                                  }
                  
                              }
                          }
                  
                          private void CreateIfMissing()
                          {
                              Directory.CreateDirectory(site_profile_path);
                          }
                  
                          private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
                          {
                              // Get the subdirectories for the specified directory.
                              DirectoryInfo dir = new DirectoryInfo(sourceDirName);
                              DirectoryInfo[] dirs = dir.GetDirectories();
                  
                              if (!dir.Exists)
                              {
                                  throw new DirectoryNotFoundException(
                                      "Source directory does not exist or could not be found: "
                                      + sourceDirName);
                              }
                  
                              // If the destination directory doesn't exist, create it. 
                              if (!Directory.Exists(destDirName))
                              {
                                  Directory.CreateDirectory(destDirName);
                              }
                  
                              // Get the files in the directory and copy them to the new location.
                              FileInfo[] files = dir.GetFiles();
                              foreach (FileInfo file in files)
                              {
                                  string temppath = Path.Combine(destDirName, file.Name);
                                  file.CopyTo(temppath, false);
                              }
                  
                              // If copying subdirectories, copy them and their contents to new location. 
                              if (copySubDirs)
                              {
                                  foreach (DirectoryInfo subdir in dirs)
                                  {
                                      string temppath = Path.Combine(destDirName, subdir.Name);
                                      DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                                  }
                              }
                          }
                  
                          public bool PassFileOrFolder(string input, List<string> library)
                          {
                              foreach (string name in library)
                              {
                                  if (input.Contains(name))
                                  {
                                      return true;
                                  }
                              }
                              return false;
                          }
                      }
                  }
                  

                  请注意,我还实现了一种清理所有配置文件的方法CleanUpOldProfiles

                  Please note that I have also implemented a method to clean up all profiles CleanUpOldProfiles

                  查看代码,更改目录等.完成后 - 进行以下调用:

                  Review the code, make changes to directories etc. After done - make a following call:

                  CoreDriver something = new CoreDriver(); // creating an object
                  // settings
                  something.my_port = 50150; // multiple chrome instances - will be run on different ports
                  // I am currently having 4 chrome profiles ;)
                  something.my_name = "mynewprofile"; // full profile name will be: 'profile + my_name'. Check the code of the object.
                  // void
                  something.ConfigureProfile(); // creating new profile or updating existing one, if folder eists
                  something.Initialize(); // starting the browser
                  

                  抱歉,答案很长.希望它能以某种方式帮助你们:)

                  sorry for a long answer. Hope it helps you guys somehow :)

                  这篇关于对不同的 ChromeDriver 实例使用相同的 chrome 配置文件(会话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Selenium WebDriver 在自定义路径下载文件 下一篇:Selenium - 过时的元素参考:元素未附加到页面

                  相关文章

                  <legend id='OUHdz'><style id='OUHdz'><dir id='OUHdz'><q id='OUHdz'></q></dir></style></legend>

                • <tfoot id='OUHdz'></tfoot>
                • <i id='OUHdz'><tr id='OUHdz'><dt id='OUHdz'><q id='OUHdz'><span id='OUHdz'><b id='OUHdz'><form id='OUHdz'><ins id='OUHdz'></ins><ul id='OUHdz'></ul><sub id='OUHdz'></sub></form><legend id='OUHdz'></legend><bdo id='OUHdz'><pre id='OUHdz'><center id='OUHdz'></center></pre></bdo></b><th id='OUHdz'></th></span></q></dt></tr></i><div id='OUHdz'><tfoot id='OUHdz'></tfoot><dl id='OUHdz'><fieldset id='OUHdz'></fieldset></dl></div>

                      <bdo id='OUHdz'></bdo><ul id='OUHdz'></ul>

                    <small id='OUHdz'></small><noframes id='OUHdz'>