Selenium調用Chrome,Firefox,IE


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM