zoukankan      html  css  js  c++  java
  • 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的属性是忽略证书。

    //设置代理参数
    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; } }
  • 相关阅读:
    【转】为什么要报考系统架构设计师考试
    前端三大主流框架中文文档
    零散知识点-类的区别;函数式编程的简单总结;
    window.location相关方法
    Hybrid相关
    php中file_get_contents与curl的区别
    三级下拉菜单
    微信生成带参数二维码及响应操作
    开发中因长时间不用而遗忘的,持续补充中。。
    项目中用到的几个工具函数
  • 原文地址:https://www.cnblogs.com/AllenRandolph/p/9760942.html
Copyright © 2011-2022 走看看