selenium 浏览器启动配置 (IE、Chrome、火狐)
一般在启动浏览器的时候,直接进行new ChromeDriver()就表示启动相关类型的浏览器,这样比较简单。如果想要更进一步的设置,则需要对浏览器的启动配置项进行设置。因为selenium webdriver是基于Firefox开发的。2.0版本之前不需要相关driver进行驱动,3.0时做了改动,火狐浏览器也需要相关driver进行驱动
下面是driver下载路径
chromedriver:
http://npm.taobao.org/mirrors/chromedriver/
ieDrvier:
http://selenium-release.storage.googleapis.com/index.html
IE启动注意点:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, “-private”);
“InPrivate 浏览”可帮助阻止 Internet Explorer 存储你的浏览会话的数据。这包括 Cookie、Internet 临时文件、历史记录以及其他数据。默认情况下将禁用工具栏和扩展。有关详细信息,请参阅帮助。
关闭此浏览器窗口,以关闭 InPrivate 浏览。
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
火狐启动注意点:
火狐的profile安装的位置,在help–》troubleshooting information–》show folder
webdriver.accept.untrusted.certs的属性是忽略证书。
FilePath.getdriverDirectory()是获取driver的路径,可自行配置,
public class lanchBrowser { static Logger logger = Logger.getLogger(lanchBrowser.class ); public static WebDriver driver=null; public static FirefoxProfile firefoxProfile=null; public static DesiredCapabilities caps=null;
//此处表示启动node节点下的浏览器。后续会讲解,暂时用不到 public static String nodeurl="";
//根据传入的类型来其他不同的浏览器 public static WebDriver getBrowserDriver(String browserType) { switch (browserType) { case "firefox":
System.setProperty("webdriver.firefox.bin",FilePath.getdriverDirectory()+"FireFsoxDriverServer.exe"); firefoxProfile.setPreference("webdriver.accept.untrusted.certs", "true"); caps.setCapability(FirefoxDriver.PROFILE,firefoxProfile); if (nodeurl.equals("")) { driver=new FirefoxDriver(firefoxProfile); }else { try { driver=new RemoteWebDriver(new URL(nodeurl), caps); } catch (MalformedURLException e) { e.printStackTrace(); } } logger.info("runDriver is firefox.Open FireFox browser....."); break; case "ie": if (File.separator.equals("\\")) { System.setProperty("webdriver.ie.driver", FilePath.getdriverDirectory()+ "IEDriverServer.exe"); } else if (File.separator.equals("/")) { System.setProperty("webdriver.ie.driver", FilePath.getdriverDirectory() + "IEDriverServer.exe"); } caps=DesiredCapabilities.internetExplorer(); caps.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, false); caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); caps.setCapability(InternetExplorerDriver.IE_SWITCHES, ".private"); if (nodeurl.equals("")) { driver=new InternetExplorerDriver(caps); }else { try { driver=new RemoteWebDriver(new URL(nodeurl), caps); } catch (MalformedURLException e) { e.printStackTrace(); } } logger.info("runDriver is ie.Open IE browser....."); break; case "chrome": if (File.separator.equals("\\")) { System.setProperty("webdriver.chrome.driver", FilePath.getdriverDirectory()+ "chromedriver.exe"); } else if (File.separator.equals("/")) { System.setProperty("webdriver.chrome.driver", FilePath.getdriverDirectory() + "chromedriver"); } new DesiredCapabilities(); caps=DesiredCapabilities.chrome(); caps.setCapability("chrome.switches",Arrays.asList("--start-maximized")); // caps.setCapability("chrome.switches",Arrays.asList("--proxy-server=http://url:port"));设置代理 if (nodeurl.equals("")) { driver=new ChromeDriver(caps); }else { try { driver=new RemoteWebDriver(new URL(nodeurl), caps); } catch (MalformedURLException e) { e.printStackTrace(); } } logger.info("runDriver is chrome.Open Chrome browser....."); break; } return driver; } }