当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-10 23:41:07 C#

C#学习教程:对不同的ChromeDriver实例使用相同的chrome配置文件(session)我正在做这样的:namespaceXXX{publicclassCoreDriver{publicIWebDriverdriver;publicintmy_port{得到;放;}publicvoidInitialize(){stringchromeee="";if(my_port==50147){chromeee="C:/Users/AA/Downloads/chromedriver1/";}elseif(my_port==50148){chromeee="C:/Users/AA/Downloads/chromedriver2/";}elseif(my_port==50149){chromeee="C:/Users/AA/Downloads/chromedriver3/";}elseif(my_port==50140){chromeee="C:/Users/AA/Downloads/chromedriver4/";}ChromeOptions选项=newChromeOptions();options.AddArgument("user-data-dir=C:\Users\AA\AppData\Local\Google\Chrome\UserData");vardriverService=ChromeDriverService.CreateDefaultService(chromeee);driverService.HideCommandPromptWindow=true;driverService.Port=my_port;driver=newChromeDriver(driverService,options);driver.Manage().Timeouts().ImplicitlyWait(newTimeSpan(0,0,12));driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(13));//司机导航}}}调用:CoreDriverA1=newCoreDriver();A1.my_port=50147;A1.初始化();CoreDriverA2=newCoreDriver();A2.my_port=50148;A2.初始化();//超时错误在这里//...不幸的是,在第二个窗口打开后-显示超时错误:WebDriver.dll中发生类型为“OpenQA.Selenium.WebDriverException”的第一次机会异常附加信息:远程WebDriver服务器对URLhttp:/loca1host:50148/session的HTTP请求在此行60秒后超时:driver=newChromeDriver(driverService,options);使用不同的参数重新运行测试后,我发现由于指定的Chrome配置文件显示Error:options.AddArgument("user-data-dir=C:\Users\AA\AppData\Local\Google\Chrome\User数据”);如果我删除该行-那么我的cookie都不在ChromeDriver实例中,那不是我可以使用的东西:)有没有办法在多个chromedriver实例中使用相同的chrome配置文件?好的,所以我正在使用上面的方法。我的要求是:这是几句话的逻辑。这是代码。您可能想要调整一件事或另一件事。使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用系统数据;使用系统绘图;使用System.Linq;使用系统文本;使用System.Windows.Forms;使用OpenQA.Selenium;使用OpenQA.Selenium.Chrome;使用OpenQA.Selenium.Firefox;使用OpenQA.Selenium.Support.UI;使用OpenQA.Selenium.Internal;使用OpenQA.Selenium.Remote;使用System.IO;使用System.Drawing.Imaging;使用系统管理;使用System.Text.RegularExpressions;使用系统线程;使用系统诊断;使用System.Reflection;使用System.Threading.Tasks;使用System.Collections.Concurrent;使用System.Runtime.InteropServices;使用System.Net;命名空间NAMESPACE{公共类CoreDriver{公共IWebDriver驱动程序;公共字符串my_name{得到;放;}publicintmy_port{得到;放;}publicstringdefault_profile_dir=@"C:UsersUSERNAMEAppDataLocalGoogleChrome";公共字符串chromedriver_path=@"C:UsersUSERNAMEDownloadschromedriver_win32";publicstringsite_profile_path;publicstringsite_profile_path_s;publicstringdefault_path;publicvoidInitialize(){ChromeOptionsoptions=newChromeOptions();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");//禁用闪存vardriverService=ChromeDriverService.CreateDefaultService(chromedriver_path);driverService.HideCommandPromptWindow=true;driverService.Port=my_port;driver=newChromeDriver(driverService,options);driver.Manage().Timeouts().ImplicitlyWait(newTimeSpan(0,0,14));driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(15));IJavaScriptExecutorjscript=driverasIJavaScriptExecutor;jscript.ExecuteScript("returnwindow.stop");}publicvoidConfigureProfile(){site_profile_path_s=default_profile_dir+"profile"+m你的名字;site_profile_path=site_profile_path_s+@"Default";default_path=default_profile_dir+@"UserDataDefault";如果(!Directory.Exists(site_profile_path)){CreateBlankProfile();}else{//复制现有的chrome配置文件。保留缓存、扩展等。CopyProfileFiles();//但远离打开的标签RemoveOpenedTabsFiles();}}publicvoidCleanUpOldProfiles(){DirectoryInfodi=newDirectoryInfo(default_profile_dir);DirectoryInfo[]directories=di.GetDirectories("profile*",SearchOption.TopDirectoryOnly);if(directories.Count()>0){foreach(varfolderindirectories){try{Directory.Delete(folder.FullName,true);}catch{}}}}publicvoidCreateBlankProfile(){//新配置文件目录CreateIfMissing();//复制现有的chrome配置文件。保留缓存、扩展等。//但远离打开的选项卡CopyProfileFiles();复制个人资料文件夹();}publicvoidCopyProfileFiles(){//默认配置文件位置DirectoryInfodi=n新目录信息(默认路径);//复制文件列表file_lib=newList(){"Cookies","Login","Preferences","Secur"};FileInfo[]files=di.GetFiles("*",SearchOption.TopDirectoryOnly);if(files.Count()>0){foreach(varfileinfiles){if(PassFileOrFolder(file.Name,file_lib)){file.CopyTo(site_profile_path+@""+file.Name,true);}}}}publicvoidRemoveOpenedTabsFiles(){//默认配置文件位置DirectoryInfodi=newDirectoryInfo(site_profile_path);//复制文件列表file_lib=newList(){"Current","Last"};FileInfo[]files=di.GetFiles("*",SearchOption.TopDirectoryOnly);if(files.Count()>0){foreach(varfileinfiles){if(PassFileOrFolder(file.Name,file_lib)){File.Delete(file.FullName);}}}}publicvoidCopyProfileFolders(){//默认配置文件位置DirectoryInfodi=newDirectoryInfo(default_path);//复制文件夹列表folder_lib=newList(){"databases","Extension","Storage","WebApplications","FileSystem","IndexedDB"};DirectoryInfo[]directories=di.GetDirectories("*",SearchOption.TopDirectoryOnly);if(directories.Count()>0){foreach(目录中的var文件夹){如果(PassFileOrFolder(folder.Name,folder_lib)){DirectoryCopy(folder.FullName,site_profile_path+@""+folder.Name,true);}}}}privatevoidCreateIfMissing(){Directory.CreateDirectory(site_profile_path);}privatestaticvoidDirectoryCopy(stringsourceDirName,stringdestDirName,boolcopySubDirs){//获取指定目录的子目录DirectoryInfodir=newDirectoryInfo(sourceDirName);DirectoryInfo[]dirs=dir.GetDirectories();if(!dir.Exists){thrownewDirectoryNotFoundException("Sourcedirectorydoesnotexistorcouldnotbefound:"+sourceDirName);}//如果目标目录不存在,则创建它。if(!Directory.Exists(destDirName)){Directory.CreateDirectory(destDirName);}//获取目录中的文件并将它们复制到新位置。FileInfo[]文件=dir.GetFiles();foreach(文件中的FileInfo文件){stringtemppath=Path.Combine(destDirName,file.Name);file.CopyTo(temppath,false);}//如果复制子目录,则将它们及其内容复制到新位置。if(copySubDirs){foreach(DirectoryInfosubdirindirs){stringtemppath=Path.Combine(destDirName,subdir.Name);DirectoryCopy(subdir.FullName,temppath,copySubDirs);}}}publicboolPassFileOrFolder(stringinput,Listlibrary){foreach(stringnameinlibrary){if(input.Contains(name)){returntrue;}}返回假;请注意,我还实现了一种方法来清理所有配置文件CleanUpOldProfiles查看代码、更改目录等完成后-进行以下调用:CoreDriversomething=newCoreDriver();//创建一个对象//设置something.my_port=50150;//多个chrome实例-将在不同的端口上运行//我目前有4个chrome配置文件;)something.my_name="mynewprofile";//完整的个人资料名称将是:'profile+my_name'。检查对象的代码。//voidsomething.ConfigureProfile();//创建新配置文件或更新现有配置文件,如果文件夹存在something.Initialize();//启动浏览器很抱歉回答很长。希望对大家有所帮助:)以上就是关于C#学习教程的全部内容:Usingthesamechromeprofile(session)fordifferentChromeDriverinstances如果对大家有用,需要进一步了解C#学习教程,请分享所有内容,希望大家多多关注——本文整理自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: