Selenium启动不同浏览器

1、启动Chrome "webdriver.chrome.driver"

System.setProperty("webdriver.chrome.driver", "files\chromedriver.exe");  //指定驱动路径

WebDriver driver = new ChromeDriver();

加载插件及使用用户默认浏览器配置

System.setProperty("webdriver.chrome.driver", "files\chromedriver.exe");

File file = new File ("files\youtube.crx");//获取插件

ChromeOptions options = new ChromeOptions();

options.addExtensions(file);//加载插件

options.addArguments("user-data-dir=C:/Users/test/AppData/Local/Google/Chrome/User Data");//制定配置文件地址

WebDriver driver = new ChromeDriver(options);

2、启动Firefox  "webdriver.firefox.bin"

System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe");  

WebDriver driver = new FirefoxDriver();

启动浏览器时加载插件

System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");

File file = new File("files/firebug-2.0.7-fx.xpi");

FirefoxProfile profile = new FirefoxProfile();

try {

  profile.addExtension(file);

} catch (IOException e) {

  e.printStackTrace();

}
profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
//active firebug extensions
profile.setPreference("extensions.firebug.allPagesActivation", "on");   
WebDriver driver = new FirefoxDriver(profile);

启动firefox时设置profile:
String proxyIp = "10.17.171.11";
int proxyPort = 8080;
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin",
        "D:/Program Files/Mozilla Firefox/firefox.exe");
FirefoxProfile profile = new FirefoxProfile();
//设置代理参数
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
//设置默认下载路径
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\");
WebDriver driver = new FirefoxDriver(profile);

启动本机器的firefox配置

System.setProperty("webdriver.firefox.bin",

"D:/Program Files/Mozilla Firefox/firefox.exe");

ProfilesIni pi = new ProfilesIni();

FirefoxProfile profile = pi.getProfile("default");

WebDriver driver = new FirefoxDriver(profile);

查看更多配置项可打开firefox,输入about:config。

3、启动IE  "webdriver.ie.driver"

System.setProperty("webdriver.ie.driver", "files\IEDriverServer.exe");

WebDriver driver = new InternetExplorerDriver();

关闭IE保护模式

System.setProperty("webdriver.ie.driver", "files\IEDriverServer.exe");

DesiredCapabilities dc = DesiredCapabilities.internetExplorer();

dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 

//IE默认启动保护模式,要么手动在浏览器的设置中关闭保护模式,要么在代码中加上这一句,即可

dc.setCapability("ignoreProtectedModeSettings", true);

WebDriver driver = new InternetExplorerDriver(dc);

未完待续。。。。。。。。

原文地址:https://www.cnblogs.com/swordyt/p/5375798.html