1. 程式人生 > >selenium 瀏覽器啟動配置 (IE、Chrome、火狐)

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 folderwebdriver.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; } }