Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对象引用未设置为对象错误C#类selenium webdriver的实例_C#_Selenium_Selenium Webdriver - Fatal编程技术网

对象引用未设置为对象错误C#类selenium webdriver的实例

对象引用未设置为对象错误C#类selenium webdriver的实例,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我有一个名为Driver的类,我试图将它的属性添加到另一个类中,这样我就可以将它用于定位器,但它在定位器的驱动程序上给了我这个错误 using OpenQA.Selenium; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.UI; namespace AutomationTest { public class BaseLocator { public static RemoteWebDriver

我有一个名为
Driver
的类,我试图将它的属性添加到另一个类中,这样我就可以将它用于定位器,但它在定位器的驱动程序上给了我这个错误

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

namespace AutomationTest
{
   public class BaseLocator
   {
       public static RemoteWebDriver driver;
       public static WebDriverWait wait;
       public IWebElement SearchBox => driver.FindElementByCssSelector("#twotabsearchtextbox");
       public IWebElement SearchButton => driver.FindElementByCssSelector("span#nav-search-submit-text + input");
       public IWebElement GoToShoppingButton => driver.FindElementByCssSelector("#nav-cart-count");
       public IWebElement GoToYourAmazonButton => driver.FindElementByCssSelector("a#nav-your-amazon");
   }
}
我已将
驱动程序设置为:

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

namespace AutomationTest
{
   public class Driver
   {
       public RemoteWebDriver SetUp()
       {
           RemoteWebDriver driver = new ChromeDriver();
           return driver;
       }
   }
}

您需要初始化
驱动程序
。除非您计划在驱动程序类中设置多个内容,否则这是不必要的(不要过度考虑)

此示例初始化与定义内联的
驱动程序
对象

使用OpenQA.Selenium;
使用OpenQA.Selenium.Chrome;
使用OpenQA.Selenium.Remote;
使用OpenQA.Selenium.Support.UI;
名称空间自动测试
{
公共类基址定位器
{
公共静态RemoteWebDriver驱动程序=新的ChromeDriver();
//public static WebDriverWait wait;//TODO:如上所述初始化变量
public IWebElement SearchBox=>driver.FindElementByCssSelector(“#twotabsearchtextbox”);
public IWebElement SearchButton=>driver.FindElementByCssSelector(“span#导航搜索提交文本+输入”);
public IWebElement GoToShoppingButton=>driver.FindElementByCssSelector(“#导航车计数”);
public IWebElement GoToYourAmazonButton=>driver.FindElementByCssSelector(“a#nav your amazon”);
}
}
虽然此示例通过默认构造函数方法对其进行初始化:

使用OpenQA.Selenium;
使用OpenQA.Selenium.Chrome;
使用OpenQA.Selenium.Remote;
使用OpenQA.Selenium.Support.UI;
名称空间自动测试
{
公共类基址定位器
{
公共静态RemoteWebDriver;
//公共静态WebDriverWait等待;
公共BaseLocator()
{
驱动程序=新的ChromeDriver();
}
public IWebElement SearchBox=>driver.FindElementByCssSelector(“#twotabsearchtextbox”);
public IWebElement SearchButton=>driver.FindElementByCssSelector(“span#导航搜索提交文本+输入”);
public IWebElement GoToShoppingButton=>driver.FindElementByCssSelector(“#导航车计数”);
public IWebElement GoToYourAmazonButton=>driver.FindElementByCssSelector(“a#nav your amazon”);
}
}

如果您不需要<代码>驱动程序>代码> >代码> > BaseLoCutos/Cux>类,那么您应该考虑将其私有化。也不确定它是否需要声明为静态成员

我不熟悉Selenium,所以我从C#语言的角度来研究它——但它应该会让你走上正轨


关于构造函数的推荐阅读:

你所说的“五个属性”是什么意思?据我所知,你没有初始化驱动程序。。。(或“司机”)。。。而且你从来没有调用过“SetUp”方法。非常感谢你,它确实和构造函数一起工作过!