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

 

转载于:https://www.cnblogs.com/AllenRandolph/p/9760942.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值