Hi
I have below code and i want _driver to be public variable
IWebDriver _driver = new ChromeDriver();
C#
static void Main(string[] args)
{
IWebDriver _driver = new ChromeDriver();
_driver.Navigate().GoToUrl("google.com");
_driver.FindElement(By.Name("q")).SendKeys("Learn Automation");
}Thanks

Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 8, 2024, 5:11 AM EDT
To make the
_drivervariable a public variable accessible throughout the class, you can declare it outside theMainmethod and mark it aspublic.using OpenQA.Selenium; using OpenQA.Selenium.Chrome; public class Program { public static IWebDriver _driver = new ChromeDriver(); static void Main(string[] args) { _driver.Navigate().GoToUrl("https://www.google.com"); _driver.FindElement(By.Name("q")).SendKeys("Learn Automation"); } }