我的代码如下:
public Form1()
{
InitializeComponent();
ChromeOptions options = new ChromeOptions();
IWebDriver driver = new ChromeDriver(options);
options.AddArgument("--user-data-dir=myDir");
}
private void button1_Click(object sender, EventArgs e)
{
}
我想在driver
button1_Click
答案 0 :(得分:2)
这是不可能的,因为它限定在你的方法中。因此,当您离开该方法时,变量超出此范围时会收集数据。您需要一个声明如下的实例变量:
public class Form1
{
private readonly IWebdriver _driver;
public Form1()
{
InitializeComponent();
ChromeOptions options = new ChromeOptions();
this._driver = new ChromeDriver(options);
options.AddArgument("--user-data-dir=...");
}
private void button1_Click(object sender, EventArgs e)
{
// do someting with your driver
this._driver...
}
}