Start learning selenium in order to start Firefox can be laborious, in the help of the great God only to be done, out of the selenium first step: jdk1.8 + selenium_2.46 + Firefox International version 40.0.3.
1, selenium start Firefox, the default launch a brand-new, do not load any personal data browser, this is also the simplest:
public void Startfirefox () {driver = new firefoxdriver (); System.out.println ("Startfirefox.");
Of course if Firefox is not installed in the default path this requires us to manually set the Firefox boot path:
2, the problem comes, if we want to let the browser start with an extension I want, or directly follow my profile to start, what should we do? The solution is very simple, but also very user-friendly, that is to load the configuration file!! First we need a new Firefox profile object: Firefoxprofile, and then add what we need into this file object, either a plugin or an existing configuration file:
Firefoxprofile profile = new Firefoxprofile (); Create a Firefox profile object { //Create an extension or profile object that needs to be added //will need to be expanded, existing profiles, etc. added to profile, Even directly modify the profile of the Firefox parameters; } firefoxdriver driver = new Firefoxdriver (profile); Start Firefox with profile object created
- Load a specific plug-in at startup, such as Firebug
public void Startfirefoxwithplug () {File Plugfile = new File ("File/firebug_2.0.12.xpi"); Firefoxprofile profile = new Firefoxprofile (); try {profile.addextension (plugfile);} catch (IOException e) {//TODO auto-g Enerated catch Blocke.printstacktrace ();} Driver = new Firefoxdriver (profile); System.out.println ("Startfirefoxwithplug.");
- Load the default local profile at startup, and when loading the default local profile, a configuration file needs to be initialized first: default
public void Startfirefoxwithprofile () {Profilesini profiles = new Profilesini (); Firefoxprofile profile = new Firefoxprofile ();p rofile = Profiles.getprofile ("Default");d river = new Firefoxdriver ( Profile); System.out.println ("Startfirefoxwithprofile.");
- Load additional configuration files at startup:
public void Startfirefoxwithotherprofile () {File Profiledir = new File ("Profiles/34t8j0sz.default"); Firefoxprofile profile = new Firefoxprofile (profiledir);d river = new Firefoxdriver;}
- Set the browser's parameters at startup to set the proxy as an example:
public void Setproxyoffirefox () {firefoxprofile-profile = new Firefoxprofile ();p rofile.setpreference (" Network.proxy.type ", 1);p rofile.setpreference (" Network.proxy.http "," Proxyip ");p rofile.setpreference (" Network.proxy.http_port "," ProxyPort ");d river = new Firefoxdriver; System.out.println ("Setdownloaddiroffirefox.");
The Setpreference method in this code is used to set the browser parameters, Network.proxy.type is the configuration agent in Firefox corresponding fields, these fields can be seen in Firefox about:config;
In fact, setting up Firefox startup is very simple, just remember that the various scenes of Firefox startup are set around this configuration file (profile), so that you do not need to rote so much code, know that the heart will naturally.
Selenium startup Firefox in various scenarios