Reprint Address: http://www.cnblogs.com/puresoul/p/4251536.html
This article mainly records how to start a variety of browsers when using Selenium2/webdriver, and how to load plug-ins, customize browser information (set profile), etc.
Environmental construction can refer to my another article: http://www.cnblogs.com/puresoul/p/3483055.html
First, driver download address :
http://docs.seleniumhq.org/download/
second, launch Firefox browser (Do not need to download driver, native support)
1, Firefox installed in the default path:
1 //start the FF
2 public void Startfirefoxbydefault () {3 System.out.println under the default installation path ("Start Firefox 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 under the default path:
1 public static void Startfirefoxnotbydefault () {
2 System.out.println ("Start Firefox browser ...");
3 system.setproperty ("Webdriver.firefox.bin",//Specify Firefox installation path
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, start Firefox loading plug-ins:
First, you know why we need to load plug-ins. The reason for this is that when Webdriver starts a browser, it launches a clean browser with no tasks, plugins, and cookies (even if you have some plugins installed on your native Firefox, Webdriver startup Firefox does not have these plugins), However, it is possible to test the system itself requires plug-ins or need to debug, etc., at this time can be used to launch Firefox when the plug-in load, the following example load Firebug plug-ins:
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 = 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, Start Firefox settings profile:
The above mentioned Webdriver start Firefox is to start a completely new browser, we can use the method mentioned above to customize the plug-in, Webdriver can also customize the profile (in the Firefox address bar, enter about: Config, you can view the Firefox parameters), set the proxy and default download path below:
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 ten Profile.setpreference ("Network.proxy.type", 1);
One 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 ..."); }
5, the launch of the machine's Firefox configuration:
Every time you start to configure profile in your code like the one above, you can use the following methods to start the Firefox configuration of this machine, in other words, we can configure Firefox in advance and then start it with Webdriver, So the Firefox installed on this machine what 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 a configuration, and then load:
1, will be a machine Profiles folder "C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles" to copy out to a directory
2, Code:
1 public static void Startfirefoxbyotherconfig () {
2 System.out.println ("Start Firefox browser ...");
3 system.setproperty ("Webdriver.firefox.bin",
4 "D:/program Files/mozilla firefox/firefox.exe");
5 File = new file ("Files\\lg6mie1i.default"); Profiles file directory, here I was placed in the project directory under the Files folder
6 firefoxprofile profile = new Firefoxprofile (file);
7 Webdriver Driver = new Firefoxdriver (profile);
8 driver.get ("http://www.baidu.com");
9 System.out.println ("Start Firefox browser succeed ...");
Ten }
PS: If the plugin or other things do not load successfully, you can check the profile folder contains the plugin information.
Third, start the Chrome browser
1, start the chrome need Chromedriver drive:
1 public static void Startchrome () {
2 System.out.println ("Start Firefox browser ...");
3 system.setproperty ("Webdriver.chrome.driver", "Files\\chromedriver.exe"); Specifies the drive path
4 webdriver driver = new Chromedriver ();
5 driver.get ("http://www.baidu.com/");
6 System.out.println ("Start Firefox browser succeed ...");
7 }
Alternatively, if you do not want to use the SetProperty method, you can place the Chromedriver.exe under the "C:\Windows\System32" path or path you can find and restart the computer.
2, loading Plug-ins:
1 public static void Startchromeloadplugin () {
2 System.out.println ("Start Firefox browser ...");
3 system.setproperty ("Webdriver.chrome.driver", "Files\\chromedriver.exe");
4 File = new file ("Files\\youtube.crx");
5 chromeoptions options = new Chromeoptions ();
6 options.addextensions (file);
7 Webdriver Driver = new Chromedriver (options);
8 driver.get ("http://www.baidu.com/");
9 System.out.println ("Start Firefox browser succeed ...");
Ten }
3, set profile: Not to be continued ...
Four, start IE browser
1, ie start-up and chrome similar also need to download the corresponding driver:
1 public static void Startie () {
2 System.out.println ("Start Firefox browser ...");
3 system.setproperty ("Webdriver.ie.driver", "Files\\iedriverserver.exe");
4 Webdriver Driver = new Internetexplorerdriver ();
5 driver.get ("http://www.baidu.com/");
6 System.out.println ("Start Firefox browser succeed ...");
7 }
2, ie no plug-in loading
3, IE magnification is to set 100%
4, when starting IE, you need to close the following figure in the 4 areas of protection mode:
5. For the 4th mentioned Shutdown protection mode, you can also use code to close:
1 //start IE browser and turn off protection mode
2 public static void Startieandcloseprotectedmode () {
3 System.out.println ( "Start Firefox browser ...");
4 system.setproperty ("Webdriver.ie.driver", "Files\\iedriverserver.exe");
5 desiredcapabilities dc = Desiredcapabilities.internetexplorer ();
6 dc.setcapability (Internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains, true);
7
8 //ie The default boot protection mode, either manually turn off the protection mode in the browser settings, or add this sentence to the code,
9 dc.setcapability (" Ignoreprotectedmodesettings ", true); Webdriver Driver = new Internetexplorerdriver (DC);
One driver.get ("http://www.baidu.com/"); System.out.println ("Start Firefox browser succeed ..."); }
============================firefox Firebug plug-in parameter settings (supplemental) =================================
When you use SELENIUM2 (webdriver) to start Firefox and automatically load the Firebug plug-in, when you switch to the Firebug plug-in's Network and Cookies section, the prompt panel is disabled, as shown in the following figure:
So we enter About:config in the Firefox settings page to try to find out if there are any parameters to control the panel's disable/enable, I found the following two parameters: Cookies Panel--- Extensions.firebug.cookies.enableSites Network Panel---extensions.firebug.net.enableSites
Tips:
Firefox Settings page parameters are very much, if we do not know the parameter name of a set, we can manually set the value of the Firefox interface, and then according to our customized values to find about:config inside to find, so as to find the parameters we want to set.
To set the value of these two parameters to true in code, the results are as follows:
The code is as follows:
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 = new file ("Files/firebug-1.9.0-fx.xpi");
6 Firefoxprofile profile = new Firefoxprofile ();
7 try {8 profile.addextension (file); 9} catch (IOException e) {e.printstacktrace (); 11} 12//Set Firebug version p Rofile.setpreference ("Extensi