Selenium startup Firefox in various scenarios

Source: Internet
Author: User

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
    1. 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.");
    1. 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.");
    1. 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;}
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.