Selenium启动带插件的火狐浏览器Firefox

Selenium WebDriver在启动火狐时,默认启动的是一个全新的没有安装任何插件的火狐浏览器,我们一般都是在这种情况下运行脚本的。

但是有时候我们会需要使用到火狐的一些插件,比如说脚本执行中断了,我们需要用Try Xpath(相当于之前的Firebug)等小插件来判断下元素定位等写的是不是正确,如果脚本的步骤比较长,我们手工启动电脑中平时使用的带插件的火狐浏览器,再重新操作到中断步骤的话,就会不太方便。

这个时候就可以启动带插件的火狐浏览器,启动带插件的火狐浏览器时是可以指定具体带哪个插件的,不过本文启动的是和我们手工使用时完全一样的火狐浏览器,手工使用的火狐浏览器中安装了多少插件,用本文的方法启动后就会有多少个插件,这个火狐浏览器的profile名字默认叫“default”。

注:profile中存放了火狐浏览器中很多基于用户的个性化的设置,比如插件、书签、个人喜好的首页等等,火狐浏览器是可以创建多个profile的,可以独立为它们命名,平时默认操作的profile名字叫“default”

具体思路:

1. 需要引入以下相关的类

import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

2. 实例化profile的对象:allProfiles,实例化火狐profile的对象:profile

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("default");

解释:

创建了allProfiles实例化对象后,使用此对象调用getProfile方法,指定使用“default”profile,并传给火狐实例化的对象profile。

3. 实例化FirefoxOptions类的对象

 FirefoxOptions options = new FirefoxOptions();
 options.setProfile(profile);

解释:

实例化FirefoxOptions类的对象:options,并调用setProfile方法获取到步骤2中的default profile

注意:

到这个步骤时,网上有很多之前旧版本的解决方案都是没有步骤3中的步骤,而直接用下面的语句

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("default");
WebDriver driver = new FirefoxDriver(profile);
这个红色的语句或这种构造在新的selenium java WebDriver3.6 以后已经被废弃掉了:
FirefoxDriver Constructor (FirefoxBinary, FirefoxProfile) WebDriver

Note: This API is now obsolete.

再这样写的话就会报下面的错误:

The constructor FirefoxDriver(FirefoxProfile) is undefined

所以现在新版本的webdriver都需要增加步骤3中的语句

4. 使用带参的方式启动火狐浏览器

WebDriver driver = new FirefoxDriver(options);

options中指定的就是default profile的火狐浏览器,启动时会看到和平时手工操作一样的带插件的火狐了。

【完成】

下面是完整的代码:

package learnwebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class OpenWebsite_profile {

	public static void main(String[] args) throws InterruptedException {
		
		 //定义gecko driver的获取地址
         System.setProperty("webdriver.gecko.driver", "D:\\BrowserDriver\\geckodriver.exe");
         
         
         //下面的语句用来启动带插件的火狐浏览器
         ProfilesIni allProfiles = new ProfilesIni();
         FirefoxProfile profile = allProfiles.getProfile("default");
         
         FirefoxOptions options = new FirefoxOptions();
         options.setProfile(profile);
         
        		
        //创建一个叫driver的对象,启动火狐浏览器  
        WebDriver driver = new FirefoxDriver(options);
        
        //通过对象driver调用具体的get方法来打开网页
        driver.get("http://www.eteams.cn/");
        
        //最大化浏览器窗口
        driver.manage().window().maximize();
        
        Thread.sleep(5000);
        

        //退出浏览器
        driver.quit();
       
	}

}

****************************************************************************************************

最近我会持续更新Selenium Java的相关文章,也请大家多多关注我的视频课程

全网最新、最完整、最具性价比的自动化测试课程

Selenium3 Java自动化测试完整教程

*****************************************************************************************************

猜你喜欢

转载自blog.csdn.net/yoyocat915/article/details/81772422