一般在啟動瀏覽器的時候,直接進行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的屬性是忽略證書。
//設置代理參數
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:\\");
有需要的時候在進行配置
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; } }
