C# SELENIUM:设置自定义 FIREFOX 配置文件后无法访问 URL
C# SELENIUM: Can't access URL after setting custom FIREFOX profile
我正在尝试连接到配置文件,它已成功连接到自定义 firefox 配置文件,但之后的问题是命令 FirefoxDriver driver = new FirefoxDriver(options);不再有效,仅当我删除选项然后没有自定义配置文件时才有效。
最后一行 returns 错误 OpenQA.Selenium.WebDriverException: 'Process unexpectedly closed with status 0' 或对远程 WebDriver 的 HTTP 请求在 60 秒后超时,只有在我删除 FirefoxDriver 中的选项时它才有效: FirefoxDriver driver = new FirefoxDriver(options);
此外,执行 options.AddArgument("-profile" + "C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profilesk2mdm2k.myprofile");
而不是拆分 2 个参数不会在正确的配置文件中启动 firefox。
甚至 options.AddArgument("no-sandbox")
或 options.AddArgument("-no-sandbox")
或 options.AddArgument("--no-sandbox")
都不起作用,而且 --profile
而不是 -profile
也无法打开正确的配置文件,无论如何,这是我的代码:
using System;
using OpenQA.Selenium; // nuget package name: Selenium.WebDriver
using OpenQA.Selenium.Firefox; // nuget package name: Selenium.WebDriver.GeckoDriver
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
FirefoxOptions options = new FirefoxOptions();
options.AddArgument("-profile");
options.AddArgument(@"C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profilesk2mdm2k.myprofile"); /* type about:profiles in firefox bar to create and manage firefox profiles, from there you will see which profile used, make sure to not use the default one and use root directory */
FirefoxDriver driver = new FirefoxDriver(options); /* code stops here and puts error after closing browser or waiting until it close itself after 60 sec */
driver.Navigate().GoToUrl("https://www.google.com/"); /* can only reach this part of code if i remove turn FirefoxDriver(options); to FirefoxDriver(); on the line upper, but no more custom profile so */
}
}
}
希望你能帮助我在这一步被封锁了 3 天
使用这些 nugget 版本:
<PackageReference Include="Selenium.Firefox.WebDriver" Version="0.27.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-beta4" />
这对我有用:
using OpenQA.Selenium.Firefox;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var options = new FirefoxOptions();
var profile = new FirefoxProfile(@"C:\Users\CatalinR\AppData\Roaming\Mozilla\Firefox\Profiles\f9n067l1.default");
options.Profile = profile;
var driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("https://www.google.com/");
}
}
}
我正在尝试连接到配置文件,它已成功连接到自定义 firefox 配置文件,但之后的问题是命令 FirefoxDriver driver = new FirefoxDriver(options);不再有效,仅当我删除选项然后没有自定义配置文件时才有效。
最后一行 returns 错误 OpenQA.Selenium.WebDriverException: 'Process unexpectedly closed with status 0' 或对远程 WebDriver 的 HTTP 请求在 60 秒后超时,只有在我删除 FirefoxDriver 中的选项时它才有效: FirefoxDriver driver = new FirefoxDriver(options);
此外,执行 options.AddArgument("-profile" + "C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profilesk2mdm2k.myprofile");
而不是拆分 2 个参数不会在正确的配置文件中启动 firefox。
甚至 options.AddArgument("no-sandbox")
或 options.AddArgument("-no-sandbox")
或 options.AddArgument("--no-sandbox")
都不起作用,而且 --profile
而不是 -profile
也无法打开正确的配置文件,无论如何,这是我的代码:
using System;
using OpenQA.Selenium; // nuget package name: Selenium.WebDriver
using OpenQA.Selenium.Firefox; // nuget package name: Selenium.WebDriver.GeckoDriver
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
FirefoxOptions options = new FirefoxOptions();
options.AddArgument("-profile");
options.AddArgument(@"C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profilesk2mdm2k.myprofile"); /* type about:profiles in firefox bar to create and manage firefox profiles, from there you will see which profile used, make sure to not use the default one and use root directory */
FirefoxDriver driver = new FirefoxDriver(options); /* code stops here and puts error after closing browser or waiting until it close itself after 60 sec */
driver.Navigate().GoToUrl("https://www.google.com/"); /* can only reach this part of code if i remove turn FirefoxDriver(options); to FirefoxDriver(); on the line upper, but no more custom profile so */
}
}
}
希望你能帮助我在这一步被封锁了 3 天
使用这些 nugget 版本:
<PackageReference Include="Selenium.Firefox.WebDriver" Version="0.27.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-beta4" />
这对我有用:
using OpenQA.Selenium.Firefox;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var options = new FirefoxOptions();
var profile = new FirefoxProfile(@"C:\Users\CatalinR\AppData\Roaming\Mozilla\Firefox\Profiles\f9n067l1.default");
options.Profile = profile;
var driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("https://www.google.com/");
}
}
}