一 、启动firefox浏览器(对于火狐浏览器不需要下载驱动,selenium原生支持)
1、firefox安装在默认路径下:
//启动默认安装路径下的ff2 public void StartFireFoxByDefault(){ WebDriver driver = new FirefoxDriver(); //new一个FirefoxDriver就OK了 driver.get("http://www.baidu.com/"); }
2、firefox未安装在默认路径下:
public static void StartFireFoxNotByDefault(){ System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe"); //指定firefox的安装路径 WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com/"); }
3、启动firefox时加载插件:
向以上两种情况webdriver在启动浏览器时,启动的一个干净的没有任务、插件及cookies信息的浏览器(计时你本机的firefox安装了某些插件,上面两种方法启动的firefox也是没有这些插件的),但是有可能需要使用firebug等插件,此时可以用如下方法在启动firefox时加载插件,下面示例加载firebug插件:
public static void StartFireFoxLoadPlugin(){ System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); File file = new File("files/firebug-2.0.7-fx.xpi"); FirefoxProfile profile = new FirefoxProfile(); try { profile.addExtension(file); } catch (IOException e) { e.printStackTrace(); } profile.setPreference("extensions.firebug.currentVersion", "2.0.7"); profile.setPreference("extensions.firebug.allPagesActivation", "on"); WebDriver driver = new FirefoxDriver(profile); driver.get("http://www.baidu.com"); }
4、启动firefox时设置profile:
除了可以使用上面提到的方法定制插件,webdriver还可以对profile进行定制(在firefox地址栏中输入about:config,可以查看firefox的参数),下面设置代理和默认下载路径:
public static void StartFireFoxByProxy(){ String proxyIp = "10.17.171.11"; int proxyPort = 8080; System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); FirefoxProfile profile = new FirefoxProfile(); //设置代理参数 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);//默认值为1,表示下载文件保存到"下载"文件夹 // 0表示用户的桌面上 //2 表示保存到设置的指定文件夹 profile.setPreference("browser.download.manager.showWhenStarting", "false"); //browser.download.manager.showWhenStarting 表示下载的时候是否显示Firefox的下载窗口 ,默认使true profile.setPreference("browser.download.dir", "D:\\");//设置下载的文件目录 //其实通过这几个就可以实现一个简单的静默下载哦!!!!! WebDriver driver = new FirefoxDriver(profile); driver.get("http://www.baidu.com"); }
5、启动本机器的firefox配置:
如果每次都在代码里面配置profile是比较麻烦的,还有一种简单的放大可以使用本机器的firefox的配置,换句话说就是我们可以事先配置本机的firefox然后用webdriver启动它,这样本机上的firefox安装了什么插件都可以直接使用了,不需要在配置profile(怎么样简单吧):
public static void StartLocalFirefox(){ System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); ProfilesIni pi = new ProfilesIni(); FirefoxProfile profile = pi.getProfile("default"); WebDriver driver = new FirefoxDriver(profile); driver.get("http://www.baidu.com/"); }
可以启动本机器的firefox配置,但是有一个弊端就是在我们执行完程序的时候如果更改了firefox而没有还原下次在执行firefox的配置是不同的,那么我们就可以在创建一个配置文件然后手动调试好,这样我们日常使用时用的是一个配置文件,selenium用得是另一个配置文件,互补干扰.具体步骤如下:
cdm进入firefox目录后的代码是: firefox.exe -ProfileManager -no-remote
6、如果在机器B上要启动机器A上的firefox配置,可以先导出A的配置,然后加载:
就是将A机器上的Profiles文件夹”给拷贝出来到某个目录,我的电脑目录是C:\Users\用户名\AppData\Roaming\Mozilla\Firefox\Profiles,如果你又创建了一个配置文件,Profiles文件夹里面应该包括两个,
一个是默认的,一个是你创建的,看文件名字就可以自知道,这里灵活应变.
public static void StartFireFoxByOtherConfig(){ System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); File file = new File("files\\lg6mie1i.default"); //profiles文件目录,这里我是放在工程目录下的files文件夹下 FirefoxProfile profile = new FirefoxProfile(file); WebDriver driver = new FirefoxDriver(profile); driver.get("http://www.baidu.com"); }
2020-02-23添加:
Firefox修改user-agent:
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("general.useragent.override","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3833.93 Safari/537.36"); FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.setProfile(profile); WebDriver driver = new FirefoxDriver(firefoxOptions);
最新版本的已经没有FirefoxDriver(org.openqa.selenium.firefox.FirefoxProfile) 这个构造器了,需要把FirefoxProfile 放到 FirefoxOptions 中,创建FirefoxDriver时使用FirefoxDriver(FirefoxOptions options)这个构造器来实现了.
上面以前写的代码已经不能直接运行,需要按照此种方法来修改,目前可用可用情况未检测.
还没有评论,来说两句吧...