C# 如何在静态void中调用元素?

C# 如何在静态void中调用元素?,c#,selenium,selenium-webdriver,selenium-chromedriver,C#,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在用c#做一个小的selenium程序。我想等待最多5秒,以互动按钮或东西,如果它是可见的。我已经为它编写了一个代码,但我不能在静态void main中调用该代码,因为它表示对象是非静态字段所必需的。我该如何解决这个问题? 错误:非静态字段、方法或属性的程序需要对象引用。WaitForPageUntileLementsVisible(By,int) 班级计划 { 公共IWebDriver static void Main(string[] args) { IW

我正在用c#做一个小的selenium程序。我想等待最多5秒,以互动按钮或东西,如果它是可见的。我已经为它编写了一个代码,但我不能在静态void main中调用该代码,因为它表示对象是非静态字段所必需的。我该如何解决这个问题? 错误:非静态字段、方法或属性的程序需要对象引用。WaitForPageUntileLementsVisible(By,int)

班级计划 { 公共IWebDriver

    static void Main(string[] args)
    {
        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://www.mail.com/int/");
        IWebElement login = driver.FindElement(By.Id("login-button"));
        login.Click();
        IWebElement email = driver.FindElement(By.Id("login-email"));
        waitForPageUntilElementIsVisible(By.Id("login-email"), 5);
        email.SendKeys("CarlosdanielGrossen95@mail.com");


    }


        public  IWebElement waitForPageUntilElementIsVisible(By locator,int maxseconds)
    {


       return new  WebDriverWait(driver, TimeSpan.FromSeconds(maxseconds))
            .Until(ExpectedConditions.ElementExists((locator)));
    }

}
}

非静态字段、方法或属性的程序需要对象引用。waitForPageUntilElementIsVisible(By,int)

将方法设置为静态

公共静态IWebElement WaitForPageUntileElementsVisible(按定位器,int-maxseconds)
{
返回新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(maxseconds))
。直到(ExpectedConditions.Element存在((定位器));
}

在单独的类中定义此方法,并使用PageWait对象调用此方法

public class PageWait
{
  public  IWebElement waitForPageUntilElementIsVisible(By locator,int maxseconds)
    {
       return new  WebDriverWait(driver, TimeSpan.FromSeconds(maxseconds))
            .Until(ExpectedConditions.ElementExists((locator)));
    }
}

class Program { 
public IWebDriver driver;

    static void Main(string[] args)
    {
        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://www.mail.com/int/");
        IWebElement login = driver.FindElement(By.Id("login-button"));
        login.Click();
        IWebElement email = driver.FindElement(By.Id("login-email"));
        new PageWait().waitForPageUntilElementIsVisible(By.Id("login-email"), 5);
        email.SendKeys("CarlosdanielGrossen95@mail.com");

    }
}

您需要使
waitForPageUntilElementIsVisible
methods静态。这次“驱动程序”表示它不能在静态中?驱动程序单词这次是红色的,位于WebDriverWait之后(错误:非静态字段、方法或属性“Program.driver”@UmutPınarbaşı同样需要对象引用,这次将
driver
设置为静态。并在使用时阅读静态和非静态成员。