【问题标题】:C# SELENIUM: Can't access URL after setting custom FIREFOX profileC# SELENIUM:设置自定义 FIREFOX 配置文件后无法访问 URL
【发布时间】:2021-10-26 20:56:47
【问题描述】:

我正在尝试连接到配置文件,它成功连接到自定义 Firefox 配置文件,但之后的问题是命令 FirefoxDriver driver = new FirefoxDriver(options);没有更多的作品,只有当我删除选项然后没有自定义配置文件时才有效。

最后一行返回错误 OpenQA.Selenium.WebDriverException: 'Process unexpectedly closed with status 0' 或 The HTTP request to the remote WebDriver timed out after 60 seconds, 只有当我删除 FirefoxDriver 中的选项时才有效:FirefoxDriver driver = new FirefoxDriver(options);

此外,执行 options.AddArgument("-profile" + "C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profiles\5k2mdm2k.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\Profiles\5k2mdm2k.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 天

【问题讨论】:

    标签: c# selenium selenium-webdriver selenium-firefoxdriver


    【解决方案1】:

    使用这些金块版本:

     <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/");   
            }
        }
    }
    

    【讨论】:

    • 我这样做了,当我继续 about:profiles 时没有使用过的配置文件,这意味着它使用了一个临时配置文件,你能不能让我看看你的 Firefox 浏览器版本,或者尝试一个与默认配置不同的配置文件查看firefox打开时使用的是否与路径对应,如果它适用于about;应该写的配置文件不能关闭,因为它已经使用了
    • 我有 Firefox 91.0.2(64 位)。我创建了一个名为 Selenium 的新配置文件,打开浏览器后,about:profiles 显示它是默认配置文件。
    • 我试过这个版本,可惜不行,你有没有这样的:imgur.com/pu0arp0
    • 其实不是,如果你看没有文字“这是正在使用的配置文件,它不能被删除。”在自动化的一个,而正常的一个,这意味着它使用临时配置文件而不是使用预制配置文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多