我猜 C# Selenium FirefoxDriver 破坏了我的代码?

C# Selenium FirefoxDriver ruining my code I guess?

我遇到了一个奇怪的错误。 这是重述。 我有一个 util class 可以将我所有的好东西存储在一个应用程序中(是的,我可以将不相关的东西存储在一个 class、代码礼仪等中),它看起来有点像像这样:

public static class util
{
    public static IWebDriver driver = new PhantomJSDriver();
    public static string spreadsheetId = "I won't show you my pantsu, senpai!";
    //more definitions and some methods here....
}

我的 Main 方法几乎是 Google 的 Quickstart 应用程序的直接副本,如下所示:

static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "I";

static void Main(string[] args)
{
    UserCredential credential;

    using (var stream =
        new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = System.Environment.GetFolderPath(
            System.Environment.SpecialFolder.Personal);
        credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
        Console.WriteLine("Credential file saved to: " + credPath);
    }

    // Create Google Sheets API service.
    var service = new SheetsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    string spreadsheetId = util.spreadsheetId;
    //BELOW IS STUFF THAT DOESN'T GET EXECUTED DUE TO EXCEPTION, READ ON!
}

所以!事实上 - 从技术上讲工作得很好。但是,由于一些与测试相关的事情,我需要将我的 util.driver 切换为 new FirefoxDriver()。你猜怎么着,当我这样做时,string spreadsheetId = util.spreadsheetId; 行抛出了异常 System.TypeInitializationException "Initializer of type .util returned an exception"。 这怎么可能? 此外,firefoxdriver 所需的 geckodriver 存在于文件夹中,但控制台确实尖叫了很多关于 "Remote host returned an error:404 not found",不知道那是什么 - PhantomJS 不会发生。

System.TypeInitializationException is an exception that is thrown as a wrapper around the exception thrown by the class initializer. This class cannot be inherited. When a class initializer fails to initialize a type, a TypeInitializationException is created and passed a reference to the exception thrown by the type's class initializer. As referred in this SO post, check your static parameter. Most commonly, a TypeInitializationException exception is thrown when a static constructor 无法实例化类型。