你调用的对象是空的

jes*_*ica 3 c# null selenium nullreferenceexception selenium-webdriver

当我在NUnit中运行这个程序时,我收到一个错误

你调用的对象是空的.

虽然这不是原始程序,但我也遇到了类似的错误.任何帮助赞赏.异常发生在

driver.Navigate().GoToUrl("http://www.yahoo.com/");
Run Code Online (Sandbox Code Playgroud)

程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;

namespace Class_and_object
{
  [TestFixture]
  public class Class1
  {
     IWebDriver driver = null;
     [Test]
     public void test1()
     {
        class2 obj = new class2();
        driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("http://www.google.com/");
        obj.method();
     }
   }
  public class class2
  {
    IWebDriver driver = null;
    public void method()
    {
        driver.Navigate().GoToUrl("http://www.yahoo.com/");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

看看你的代码:

public class class2
{
    IWebDriver driver = null;
    public void method()
    {
        driver.Navigate().GoToUrl("http://www.yahoo.com/");
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,你得到了NullReferenceException- driver总是如此null.

目前尚不清楚你希望在这里发生的事情-但也许你的意思是要通过FirefoxDriver你实例化test1method通过参数?