title: I am facing "TypeInitializationException" while executing selenium(C#) Script
body: <p>I am new to Selenium and am trying to understand different areas of it. I have come across a situation where I have 3 classes under namespace Automate_Dynamics365.</p>
  
  <ol>
  <li><p>Class <strong>POM</strong> contains all the elements in a page which are declared as public static IWebElement txtusr = driver.FindElement(By.XPath("xxxxxx”));</p></li>
  <li><p>Class <strong>Demo</strong> is where I have the actual test cases (I am using NUnit framework’s attributes like SetUp, Teardown, Test)</p></li>
  <li><p>Class <strong>Methods_Factory</strong> contains several methods which use <strong>POM</strong> class webelements.</p></li>
  </ol>
  
  <p>The below logic is not working as I am getting an error during runtime.[This is just an example] </p>
  
  <pre><code>Class POM
  {
  public static IWebElement txtusr = driver.FindElement(By.XPath("xxxxxx”));
  }
  </code></pre>
  
  <p>///////////////////////////////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>Class Methods_Factory
  {
  Public static void Login()
  {
  
  POM.txtusr.sendkeys(“YYYYY”);
  }
  }
  </code></pre>
  
  <p>/////////////////////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>Class Demo
  {
  [Test]
  Public void ex()
  {
  Methods_Factory.Login();
  }
  }
  </code></pre>
  
  <p>Error:  <a href="https://i.stack.imgur.com/W2Z9n.png" rel="nofollow noreferrer">Exception Screenshot</a></p>
  
  <p>///////////////////////////////////////////////////////////////////////////////</p>
  
  <p><strong>The below logic is working</strong></p>
  
  <pre><code>Class Methods_Factory
  {
  Public static void Login()
  {
  driver.FindElement(By.XPath("xxxxxx”)).sendkeys(“YYYYY”);
  }
  }
  </code></pre>
  
  <p>///////////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>Class Demo
  {
  [Test]
  Public void ex()
  {
  Methods_Factory.Login();
  }
  }
  </code></pre>
  
  <p>Please help me understand how do deal with this situation.</p>
  
  <p>All I am trying to do is to maintain all the page elements in one class, methods in another class which would use the Iwebelements from the previous class.</p>
  
  <p>Thanks in advance.</p>