Webdriver Start Browser, set profile& load plug-in steps

Source: Internet
Author: User

This article mainly records how to launch various browsers when using Selenium2/webdriver, and how to load plugins, customize browser information (set profile), etc.

Environment construction can refer to my other article: http://www.cnblogs.com/puresoul/p/3483055.html

One, driver download address:

http://docs.seleniumhq.org/download/

Second, start the Firefox browser (do not need to download the driver, native support)

1, Firefox installed in the default path:

1     //start the FF
2 public     void Startfirefoxbydefault () {
3         System.out.println ("Start Firefox") under the default installation path Browser ... ");
4         Webdriver Driver = new Firefoxdriver (); Direct new one firefoxdriver can be
5         Navigation Navigation = Driver.navigate ();
6         navigation.to ("http://www.baidu.com/");
7         System.out.println ("Start Firefox browser succeed ...");        
8     }

2. Firefox is not installed in the default path:

1 public static void Startfirefoxnotbydefault () {
2         System.out.println ("Start Firefox browser ...");
3         system.setproperty ("Webdriver.firefox.bin",//Specify the installation path for 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. When you start Firefox, load the plugin:

First, you know why we need to load plugins. The reason is that Webdriver launch a browser when the launch of a clean browser without tasks, plugins and cookies information (even if you have Firefox installed some plugins, Webdriver boot Firefox is not the plug-ins), However, it is possible to test the system itself requires plug-ins or need to debug, and so on, you can use the following methods when you start Firefox loading plug-ins, the following example loads the Firebug plugin:

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) {             e.printstacktrace ();
One         }         profile.setpreference ("Extensions.firebug.currentVersion", "2.0.7");         //active firebug Extensions         profile.setpreference ("Extensions.firebug.allPagesActivation", " On ");         Webdriver Driver = new Firefoxdriver (profile);         driver.get ("http://www.baidu.com");         System.out.println ("Start Firefox browser succeed ...");    
(+     }

4. Set profile when starting Firefox:

As mentioned above Webdriver start Firefox is a completely new browser, we can use the above mentioned methods to customize the plugin, Webdriver can also be customized to the profile (in the Firefox address bar, enter About:config, Can see the parameters of Firefox), the following settings for the proxy and the default download path:

 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//Set Agent parameters of Profile.setpreference ("Network.proxy.type", 1);
Profile.setpreference ("Network.proxy.http", Proxyip);
Profile.setpreference ("Network.proxy.http_port", ProxyPort);
13 14//Set default download Path Profile.setpreference ("Browser.download.folderList", 2);
Profile.setpreference ("Browser.download.dir", "d:\\");
Webdriver Driver = new Firefoxdriver (profile);
Driver.get ("http://www.baidu.com");    
System.out.println ("Start Firefox browser succeed ..."); 22}

5, start the Firefox configuration of the machine:

Each boot if you like the above in the code to configure the profile is troublesome, you can use the following method to start the Firefox configuration of the machine, in other words, we can pre-configure the local Firefox and then use Webdriver to start it, In this case, the Firefox installed on the computer plug-ins can be used directly, do not need to configure 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 ...");    
Ten     }

6, if on machine B to start the Firefox configuration on machine A, you can first export the configuration of a, and then load:

1. Copy the Profiles folder "C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles" on the A machine to a directory

2. Code:

 

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.