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

2024-06-23 22:48

本文主要是介绍启动浏览器、设置profile加载插件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、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     }
复制代码

这篇关于启动浏览器、设置profile加载插件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1088496

相关文章

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

SpringBoot启动报错的11个高频问题排查与解决终极指南

《SpringBoot启动报错的11个高频问题排查与解决终极指南》这篇文章主要为大家详细介绍了SpringBoot启动报错的11个高频问题的排查与解决,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一... 目录1. 依赖冲突:NoSuchMethodError 的终极解法2. Bean注入失败:No qu

PyCharm如何设置新建文件默认为LF换行符

《PyCharm如何设置新建文件默认为LF换行符》:本文主要介绍PyCharm如何设置新建文件默认为LF换行符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm设置新建文件默认为LF换行符设置换行符修改换行符总结PyCharm设置新建文件默认为LF

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

一文带你了解SpringBoot中启动参数的各种用法

《一文带你了解SpringBoot中启动参数的各种用法》在使用SpringBoot开发应用时,我们通常需要根据不同的环境或特定需求调整启动参数,那么,SpringBoot提供了哪些方式来配置这些启动参... 目录一、启动参数的常见传递方式二、通过命令行参数传递启动参数三、使用 application.pro