C#環境下,使用Selenium調用不同的浏覽器,可以使用如下方法:
1 IWebDriver driver = null;
2 string Browser =null;
3 if (Browser.Equals("IE"))
4 {
5 InternetExplorerOptions options = new InternetExplorerOptions();
6 options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
7 driver = new InternetExplorerDriver(options);
8
9 }
10 else if (Browser.Equals("Chrome".ToUpper()))
11 {
12 driver = new ChromeDriver();
13 }
14 else
15 {
16 driver = new FirefoxDriver();
17 }
注意實現:
1、使用IE浏覽器的時候要在該項目的bin\Debug或bin\Release目錄下添加IEDriverServer.exe文件。
用nuget獲取IEDriverServer.exe:Install-Package WebDriver.IEDriverServer.win32
2、使用IE時要取消浏覽器的保護模式,添加如下代碼。
InternetExplorerOptions options = new InternetExplorerOptions(); options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
3、使用Chrome浏覽器的時候要在該項目的bin\Debug或bin\Release目錄下添加chromedriver.exe文件。
用nuget獲取chromedriver.exe:Install-Package Selenium.WebDriver.ChromeDriver