栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

我的要求是:

  • 我必须保留主要Chrome配置文件的cookie
  • 我必须保留主配置文件的扩展名
  • 我不需要主要个人资料的历史记录,打开的标签页,会话等
  • 重新启动现有的自定义配置文件后-我无需打开标签就可以将其清除

简而言之,这就是逻辑。

  • 首先,我为现有的Google Chrome浏览器配置文件指定目录。
  • 如果我们需要创建cookie(即登录某个网站),则可以在google chrome的主要个人资料上进行。
  • 完成后,关闭镶边。有些网站保存cookie的时间很长,有些则没有。因此,我们有必要在必要时重新登录主配置文件。不要打开原始镀铬!否则,ChromeDriver会引发一些警告。
  • 接下来,我的脚本将必要的文件夹和文件复制到新文件夹中。此文件夹是我们包含所有cookie的新配置文件。我的PC上所有内容的大小都约为 30兆字节
  • 如果新配置文件的文件夹已存在-则该程序将仅复制cookie文件。那应该不超过 1-2兆 的数据。

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

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

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

CoreDriver something = new CoreDriver(); // creating an object// settingssomething.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 pre of the object.// voidsomething.ConfigureProfile(); // creating new profile or updating existing one, if folder eistssomething.Initialize(); // starting the browser

很抱歉回答很长。希望它以某种方式对你们有帮助:)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391017.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号