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

Share

时间:2023-04-10 18:54:05 C#

inC#usingaspecificFirefoxprofileinSeleniumWebDriver没有C#的文档。我试过的代码如下:FirefoxProfileManagerprofileManager=newFirefoxProfileManager();FirefoxProfile配置文件=profileManager.GetProfile(profileName);驱动程序=新的FirefoxDriver(配置文件);我在Java中看到的代码使用ProfilesIni而不是FirefoxProfileManager代码,但这在C#中不可用。以这种方式设置驱动程序时,使用的selenium配置文件具有所有默认设置,而不是我试图指向的配置文件中指定的设置。我不确定我是否使用正确的方法来检索配置文件,但如果有人使用Selenium2和C#,任何信息都会有所帮助。我们使用此方法加载默认的firefox配置文件(您可以创建自定义配置文件并加载它):privateIWebDriverdriver;字符串pathToCurrentUserProfiles=Environment.ExpandEnvironmentVariables("%APPDATA%")+@"MozillaFirefoxProfiles";//配置文件的路径string[]pathsToProfiles=Directory.GetDirectories(pathToCurrentUserProfiles,"*.default",SearchOption.TopDirectoryOnly);if(pathsToProfiles.Length!=0){FirefoxProfile配置文件=newFirefoxProfile(pathsToProfiles[0]);profile.SetPreference("browser.tabs.loadInBackground",false);//设置您需要的首选项driver=newFirefoxDriver(newFirefoxBinary(),profile,serverTimeout);}else{driver=newFirefoxDriver();我们遇到的问题是无法加载配置文件。问题出在FirefoxProfile(第137行)中。它只查找user.js,Firefox中的配置文件实际上是prefs.js137>>FileprefsInModel=newFile(model,"user.js");黑客解决方案:重命名prefs.js->user.js以下对我有用。我必须专门设置“webdriver.firefox.profile”首选项才能使其正常工作。varallProfiles=newFirefoxProfileManager();if(!allProfiles.ExistingProfiles.Contains("SeleniumUser")){thrownewException("SeleniumUserfirefox配置文件不存在,请先创建它。");}varprofile=allProfiles.GetProfile("SeleniumUser");profile.SetPreference("webdriver.firefox.profile","SeleniumUser");WebDriver=newFirefoxDriver(配置文件);我有同样的问题,它不是重复的。我使用以下工作私有IWebDriver驱动程序;[设置]publicvoidSetupTest(){stringpath=@"C:UsersusernameAppDataLocalMozillaFirefoxProfilesmyi5go1k.default";FirefoxProfileffprofile=newFirefoxProfile(路径);驱动程序=新的FirefoxDriver(ffprofile);遇到同样的问题,在搜索并尝试了许多不同的组合之后,我能够让Selenium在使用RemoteWebDriver时加载特定的配置文件。网格配置我使用包含“C:ProgramFiles(x86)Javajre6binjava.exe”-jarC:DownloadsSeleniumselenium-server-standalone-2.20.0.jar-rolehub-maxSession50-Dwebdriver的批处理文件启动HUB。firefox.profile=Selenium我使用包含以下内容的批处理文件启动一个或多个节点(每个节点都有唯一的端口号):"C:ProgramFiles(x86)Javajre6binjava.exe"-jarselenium-server-standalone-2.20.0.jar-rolenode-hubhttp://127.0.0.1:4444/grid/register-browserbrowserName=firefox,platform=WINDOWS,version=11.0,maxInstances=2-maxSession2-port5555-Dwebdriver.firefox.profile=Selenium的这里的关键是这些命令的最后一部分,它需要与您创建的自定义配置文件的名称相匹配。创建WebDriver实例的代码privatereadonlyUri_remoteWebDriverDefaultUri=newUri("http://localhost:4444/wd/hub/");privateIWebDriverCreateFireFoxWebDriver(UriremoteWebDriverUri){vardesiredCapabilities=newDesiredCapabilities();desiredCapabilities.SetCapabilities(CapabilityType.BrowserName,"firefox");desiredCapabilities.SetCapability(CapabilityType.Platform,newPlatform(PlatformType.Windows));desiredCapabilities.SetCapability(CapabilityType.Version,"11.0");vardrv=newRemoteWebDriver(remoteWebDriverUri??_remoteWebDefa,desiredCapabilities);返回驱动器;注意:该函数需要与您在网格中运行的节点的函数相匹配。然后,您可以调用此方法,传入集线器的Uri,或者为null以默认为localhost。使用漫游配置文件而不是本地配置文件似乎没问题。stringpath=@"C:UsersusernameAppDataRoamingMozillaFirefoxProfilesmyi5go1k.default";FirefoxProfileffprofile=newFirefoxProfile(路径);驱动程序=新的FirefoxDriver(ffprofile);以上就是C#学习教程:UsingSeleniumWebDriverinC#如果对你有用,还需要进一步了解C#学习教程,希望大家多多关注Firefox具体配置文件分享的全部内容。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: