Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件

简介: 本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法、以及如何加载插件、定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnblogs.

本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法、以及如何加载插件、定制浏览器信息(设置profile)等

环境搭建可参考我的另一篇文章:http://www.cnblogs.com/puresoul/p/3483055.html

一、Driver下载地址

  http://docs.seleniumhq.org/download/

二、启动firefox浏览器(不需要下载驱动,原生支持)

1、firefox安装在默认路径下:

1     //启动默认安装路径下的ff
2     public void StartFireFoxByDefault(){
3         System.out.println("start firefox browser...");
4         WebDriver driver = new FirefoxDriver();      //直接new一个FirefoxDriver即可
5         Navigation navigation = driver.navigate();
6         navigation.to("http://www.baidu.com/");
7         System.out.println("start firefox browser succeed...");        
8     }

2、firefox未安装在默认路径下:

1 public static void StartFireFoxNotByDefault(){
2         System.out.println("start firefox browser...");
3         System.setProperty("webdriver.firefox.bin",     //指定firefox的安装路径
4                 "D:/Program Files/Mozilla Firefox/firefox.exe");  
5         WebDriver driver = new FirefoxDriver();
6         Navigation navigation = driver.navigate();
7         navigation.to("http://www.baidu.com/");
8         System.out.println("start firefox browser succeed...");        
9     }

3、启动firefox时加载插件:

  首先,要知道我们为什么需要加载插件?原因是webdriver在启动浏览器时,启动的一个干净的没有任务、插件及cookies信息的浏览器(即使你本机的firefox安装了某些插件,webdriver启动firefox也是没有这些插件的),但是有可能被测系统本身需要插件或者需要调试等等,此时可以用如下方法在启动firefox时加载插件,下面示例加载firebug插件:

 

 1     public static void StartFireFoxLoadPlugin(){
 2         System.out.println("start firefox browser...");
 3         System.setProperty("webdriver.firefox.bin", 
 4                 "D:/Program Files/Mozilla Firefox/firefox.exe");
 5         File file = new File("files/firebug-2.0.7-fx.xpi");
 6         FirefoxProfile profile = new FirefoxProfile();
 7         try {
 8             profile.addExtension(file);
 9         } catch (IOException e) {
10             e.printStackTrace();
11         }
12         profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
13         //active firebug extensions
14         profile.setPreference("extensions.firebug.allPagesActivation", "on");    
15         WebDriver driver = new FirefoxDriver(profile);
16         driver.get("http://www.baidu.com");
17         System.out.println("start firefox browser succeed...");    
18     }

 

4、启动firefox时设置profile:

  上面提到过webdriver启动firefox时是启动一个完全新的浏览器,我们除了可以使用上面提到的方法定制插件,webdriver还可以对profile进行定制(在firefox地址栏中输入about:config,可以查看firefox的参数),下面设置代理和默认下载路径:

 1     public static void StartFireFoxByProxy(){
 2         String proxyIp = "10.17.171.11";
 3         int proxyPort = 8080;
 4         System.out.println("start firefox browser...");
 5         System.setProperty("webdriver.firefox.bin", 
 6                 "D:/Program Files/Mozilla Firefox/firefox.exe");
 7         
 8         FirefoxProfile profile = new FirefoxProfile();
 9         //设置代理参数
10         profile.setPreference("network.proxy.type", 1);
11         profile.setPreference("network.proxy.http", proxyIp);
12         profile.setPreference("network.proxy.http_port", proxyPort);
13         
14         //设置默认下载路径
15         profile.setPreference("browser.download.folderList", 2);
16         profile.setPreference("browser.download.dir", "D:\\");
17         
18         WebDriver driver = new FirefoxDriver(profile);
19         driver.get("http://www.baidu.com");
20         
21         System.out.println("start firefox browser succeed...");    
22     }

 5、启动本机器的firefox配置: 

  每次启动如果都像上面那样在代码里面配置profile比较麻烦,可以使用下面的方法启动本机器的firefox的配置,换句话说就是我们可以事先配置本机的firefox然后用webdriver启动它,这样本机上的firefox安装了什么插件都可以直接使用了,不需要在配置profile:

 1     public static void StartLocalFirefox(){
 2         System.out.println("start firefox browser...");
 3         System.setProperty("webdriver.firefox.bin", 
 4                 "D:/Program Files/Mozilla Firefox/firefox.exe");
 5         ProfilesIni pi = new ProfilesIni();
 6         FirefoxProfile profile = pi.getProfile("default");
 7         WebDriver driver = new FirefoxDriver(profile);
 8         driver.get("http://www.baidu.com/");
 9         System.out.println("start firefox browser succeed...");    
10     }

6、如果在机器B上要启动机器A上的firefox配置,可以先导出A的配置,然后加载:

1、将A机器上的Profiles文件夹”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”给拷贝出来到某个目录

2、代码:

 1     public static void StartFireFoxByOtherConfig(){
 2         System.out.println("start firefox browser...");
 3         System.setProperty("webdriver.firefox.bin", 
 4                 "D:/Program Files/Mozilla Firefox/firefox.exe");        
 5         File file = new File("files\\lg6mie1i.default");        //profiles文件目录,这里我是放在工程目录下的files文件夹下
 6         FirefoxProfile profile = new FirefoxProfile(file);    
 7         WebDriver driver = new FirefoxDriver(profile);
 8         driver.get("http://www.baidu.com");        
 9         System.out.println("start firefox browser succeed...");    
10     }

PS:如果插件或其它东东未加载成功,可以检查下profile文件夹下是否包含插件信息。

 

三、启动chrome浏览器

 1、启动chrome需要chromedriver的驱动:

1     public static void StartChrome(){
2         System.out.println("start firefox browser...");        
3         System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");  //指定驱动路径
4         WebDriver driver = new ChromeDriver();
5         driver.get("http://www.baidu.com/");
6         System.out.println("start firefox browser succeed...");        
7     }

  另,如果不想用setProperty的方式,可以将chromedriver.exe 放在”C:\Windows\System32”路径下或者path可以找到的路径下并重启电脑即可。

2、加载插件:

 1     public static void StartChromeLoadPlugin(){
 2         System.out.println("start firefox browser...");
 3         System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");
 4         File file = new File ("files\\youtube.crx");
 5         ChromeOptions options = new ChromeOptions();
 6         options.addExtensions(file);
 7         WebDriver driver = new ChromeDriver(options);
 8         driver.get("http://www.baidu.com/");
 9         System.out.println("start firefox browser succeed...");    
10     }

3、设置profile: 未完待续 ...

 

 

四、启动IE浏览器

1、IE启动和chrome类似也需要下载相应的驱动:

1     public static void StartIE(){
2         System.out.println("start firefox browser...");        
3         System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
4         WebDriver driver = new InternetExplorerDriver();
5         driver.get("http://www.baidu.com/");
6         System.out.println("start firefox browser succeed...");        
7     }

2、IE下没有插件加载

3、IE的放大比例为要设置100%

4、启动IE时,需关闭如下图中4个区域的保护模式:

5、对于第4点提到的关闭保护模式,还可以使用代码关闭:

 1     //启动IE浏览器并关闭保护模式
 2     public static void StartIEAndCloseProtectedMode(){
 3         System.out.println("start firefox browser...");        
 4         System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
 5         DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
 6         dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
 7     
 8         //IE默认启动保护模式,要么手动在浏览器的设置中关闭保护模式,要么在代码中加上这一句,即可
 9         dc.setCapability("ignoreProtectedModeSettings", true);
10         WebDriver driver = new InternetExplorerDriver(dc);
11         driver.get("http://www.baidu.com/");
12         System.out.println("start firefox browser succeed...");        
13     }

 

目录
相关文章
|
9月前
|
弹性计算 Ubuntu Linux
一键部署OpenWebUI+Ollama到阿里云ECS,轻松运行DeepSeek!(保姆级教程)
在当今数据驱动的时代,快速部署和管理大模型成为企业的关键需求。阿里云提供了一键部署OpenWebUI+Ollama的便捷方案,支持本地大模型运行和管理。用户也可以选择连接阿里云百炼的在线模型。
一键部署OpenWebUI+Ollama到阿里云ECS,轻松运行DeepSeek!(保姆级教程)
|
存储 监控 关系型数据库
问题排查:线上MySQL启动报错:Job for mysqld.service failed because the control process exite
问题排查:线上MySQL启动报错:Job for mysqld.service failed because the control process exite
9172 0
问题排查:线上MySQL启动报错:Job for mysqld.service failed because the control process exite
|
6天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
15天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
10天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
617 215
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
859 61
|
8天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1303 157