TypeInitializationException when doing action with

2019-09-04 10:57发布

问题:

I just want to click a button, or edit an input's text, but I can't do it with this method. I'm storing the elements in a public static class, and I call them from an another public static class. If I don't use this classes, i don't get exception.

public static class SiteA
{
    static uidriver = Program.uidriver;

    public static class Functionality
    {
        public static void SomeTest()
        {
            //...
            Inputs.Buttons.Authorize.Click(); //Here I get the exception
            //Working code: uidriver.FindElement(By.Id("some_element")).Click();
            //...
        }
    }

    public static class Inputs
    {
        public static class Buttons
        {
            public static IWebElement Authorize = uidriver.FindElement(By.Id("some_element"));
        }
    }   
}

class Program
{
    public static IWebDriver uidriver;
    static void Main(string[] args)
    {
        uidriver = new FirefoxDriver();

        SiteA.Functionality.SomeTest();

    }
}

An unhandled exception of type 'System.TypeInitializationException' occurred in MyExeName.exe

Additional information: The type initializer for 'Input' threw an exception.

The inner exception contains this message:

Unable to locate element: {"method":"id","selector":"some_element"}

Another detail (I think it can be necessary):

TypeName: Input

回答1:

The problem was with the static parameter.