Selenium 实例化WebDriver

实例化不同WebDriver的时候有不同的设置,比如想在初始化Driver的时候就最大化窗口,Chrome需要使用浏览器参数start-maximized,想要在初始化Edge后清空缓存需要先使浏览器跳转到一个页面。


var options = new InternetExplorerOptions();
EdgeOptions edgeOptions = new EdgeOptions();           

switch (ConfigurationHelper.Browser)
{
    case BrowserType.Chrome:
        var chromeOption = new ChromeOptions();
        chromeOption.AddArguments("start-maximized", "no-sandbox");
        this.Driver = new ChromeDriver(chromeOption);
        break;
    case BrowserType.Firefox:
        this.Driver = new FirefoxDriver();
        break;
    case BrowserType.IE:                    
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        options.RequireWindowFocus = true;
        this.Driver = new InternetExplorerDriver(options);
        break;
    case BrowserType.Edge:
        edgeOptions.PageLoadStrategy = EdgePageLoadStrategy.Eager;
        this.Driver = new EdgeDriver(edgeOptions);
         This is by design for Edge that only first navigate to some page then clear cookie will work or else will throw exception
        this.Driver.Navigate().GoToUrl("https://www.google.com");
        break;
    case BrowserType.Safari:
        this.Driver = new SafariDriver();
        break;
    case BrowserType.PhantomJS:
        this.Driver = new PhantomJSDriver();
        break;
    default:
        throw new AdventException("Unsupported browser type.");
}



部分WebDriver的下载地址:

http://selenium-release.storage.googleapis.com/index.html

Chrome Driver:
http://chromedriver.storage.googleapis.com/index.html

PhantomJS Driver:
http://phantomjs.org/download.html


你可能感兴趣的:(Selenium 实例化WebDriver)