IWebDriver Driver { get; set; }
public void InitChrome(IWebDriver e)
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("disable-infobars");
IWebDriver driver = new ChromeDriver(options);
Driver = driver;
}
public void Watcher_Changed(object sender, FileSystemEventArgs e)
{
InitChrome(Driver);
string title = Driver.Title;
Driver.Navigate().GoToUrl("file:///" + filename);
Driver.Navigate().Refresh();
InitChrome(Driver);
}
每次我使用此代码保存文件时,都会启动一个新的Google Chrome实例,如何在保留此行Driver.Navigate().GoToUrl("file:///" + filename);
时阻止此操作?
答案 0 :(得分:0)
在这种情况下,您只需要WebDriver
的一个实例,而单例模式可以帮助您。
public static IWebDriver _driver;
public IWebDriver Driver {
get {
if(_driver == null)
{
var options = new ChromeOptions();
options.AddArgument("disable-infobars");
var _driver = new ChromeDriver(options);
}
return _driver;
}
}
public void Watcher_Changed(object sender, FileSystemEventArgs e)
{
Driver.Navigate().GoToUrl("file:///" + filename);
Driver.Navigate().Refresh();
}
答案 1 :(得分:0)
通过添加
来修复它 if (Driver == null )
{
InitChrome(Driver);
}