热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

启动浏览器、设置profile&加载插件

一、Driver下载地址:http:docs.seleniumhq.orgdownload二、启动firefox浏览器(不需要下载驱动,原生支持)1、firefox安装在默认路径下

一、Driver下载地址

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

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

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

启动浏览器、设置profile&加载插件
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     }
启动浏览器、设置profile&加载插件

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

启动浏览器、设置profile&加载插件
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     }
启动浏览器、设置profile&加载插件

3、启动firefox时加载插件:

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

 

启动浏览器、设置profile&加载插件
 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     }
启动浏览器、设置profile&加载插件

 

4、启动firefox时设置profile:

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

启动浏览器、设置profile&加载插件
 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     }
启动浏览器、设置profile&加载插件

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

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

启动浏览器、设置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     }
启动浏览器、设置profile&加载插件

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

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

2、代码:

启动浏览器、设置profile&加载插件
 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     }
启动浏览器、设置profile&加载插件

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

 

三、启动chrome浏览器

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

启动浏览器、设置profile&加载插件
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     }
启动浏览器、设置profile&加载插件

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

2、加载插件:

启动浏览器、设置profile&加载插件
 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\\you tu be.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     }
启动浏览器、设置profile&加载插件

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

 

 

四、启动IE浏览器

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

启动浏览器、设置profile&加载插件
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     }
启动浏览器、设置profile&加载插件

2、IE下没有插件加载

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

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

启动浏览器、设置profile&加载插件

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

启动浏览器、设置profile&加载插件
 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     }
启动浏览器、设置profile&加载插件

推荐阅读
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
  • http会话(session)详解by:授客QQ:1033553122会话(session)是一种持久网络协议,在用户(或用户代理)端和服务器端之间创建关联,从而起到交换数据包的作 ... [详细]
author-avatar
chuntianhuaji
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有