Start Firefox Browser.
1 This is true for Firefox installed in the default path
Webdriver Driver = new Firefoxdriver ();//Direct New one Firefoxdriver
Navigation Navigation = Driver.navigate ();
Enter Baidu homepage
Navigation.to ("http://www.baidu.com");
2 This applies to Firefox not installed in the default path
System.out.println ("Start Firefox browser ...");
System.setproperty ("Webdriver.firefox.bin",//Specify the installation path for Firefox
"D:/program Files/mozilla Firefox/firefox.exe");
Webdriver Driver = new Firefoxdriver ();
Navigation Navigation = Driver.navigate ();
Navigation.to ("http://www.baidu.com/");
3 This situation can be loaded out of Firefox plug-ins.
The first thing to know is why we need to load plug-ins because Webdriver is a clean browser that launches a browser without tasks, plugins and Cookies ( Even if you have Firefox installed some plugins webdriver boot Firefox also does not have these plug-ins, but it is possible to test the system itself requires plug-ins or need to debug, etc. at this time can be used in the following ways Loading plug-ins when starting Firefox the following example loads the Firebug plugin
Import Java.io.File;
Import java.io.IOException;
Import Org.openqa.selenium.Alert;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebDriver.Navigation;
Import Org.openqa.selenium.firefox.FirefoxDriver;
Import Org.openqa.selenium.firefox.FirefoxProfile;
public class Testdemo {
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println ("Start Firefox browser ...");
System.setproperty ("Webdriver.firefox.bin",
"C:/Program Files (x86)/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");
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 ...");
}
}
--------------------------------------
The above code does not notify the following exception
Start Firefox browser ...
Exception in thread "main" org.openqa.selenium.WebDriverException:Failed to connect to binary firefoxbinary (C:\Program F Iles (x86) \mozilla Firefox\firefox.exe) on port 7055; Process output follows:
Null
Build info:version: ' 2.53.0 ', Revision: ' 35ae25b ', Time: ' 2016-03-15 16:57:40 '
System info:host: ' xl-20150414qgdq ', IP: ' 192.168.80.6 ', Os.name: ' Windows 7 ', Os.arch: ' AMD64 ', os.version: ' 6.1 ', java. Version: ' 1.7.0_80 '
Driver Info:driver.version:FirefoxDriver
At Org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start (Newprofileextensionconnection.java : 128)
At Org.openqa.selenium.firefox.FirefoxDriver.startClient (firefoxdriver.java:271)
At Org.openqa.selenium.remote.remotewebdriver.<init> (remotewebdriver.java:119)
At Org.openqa.selenium.firefox.firefoxdriver.<init> (firefoxdriver.java:218)
At Org.openqa.selenium.firefox.firefoxdriver.<init> (firefoxdriver.java:211)
At Org.openqa.selenium.firefox.firefoxdriver.<init> (firefoxdriver.java:207)
At Org.openqa.selenium.firefox.firefoxdriver.<init> (firefoxdriver.java:124)
At Testdemo.main (testdemo.java:27)
Caused by:org.openqa.selenium.firefox.UnableToCreateProfileException:java.io.FileNotFoundException: \files\ FIREBUG-2.0.7-FX.XPI (the system cannot find the path specified.) )
Build info:version: ' 2.53.0 ', Revision: ' 35ae25b ', Time: ' 2016-03-15 16:57:40 '
System info:host: ' xl-20150414qgdq ', IP: ' 192.168.80.6 ', Os.name: ' Windows 7 ', Os.arch: ' AMD64 ', os.version: ' 6.1 ', java. Version: ' 1.7.0_80 '
Driver Info:driver.version:FirefoxDriver
At Org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk (firefoxprofile.java:427)
At Org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start (Newprofileextensionconnection.java : 85)
... 7 more
Caused by:java.io.FileNotFoundException: \FILES\FIREBUG-2.0.7-FX.XPI (the system cannot find the path specified.) )
At Java.io.FileInputStream.open (Native Method)
At java.io.fileinputstream.<init> (Unknown Source)
At Org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory (fileextension.java:80)
At Org.openqa.selenium.firefox.internal.FileExtension.writeTo (fileextension.java:59)
At Org.openqa.selenium.firefox.FirefoxProfile.installExtensions (firefoxprofile.java:443)
At Org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk (firefoxprofile.java:421)
... 8 more
4 The use of paragraph (3) was not adjusted.
Every boot if it is like above in the code to configure the profile of the trouble can use the following method to start the Firefox configuration of this machine in other words, we can pre-configure the Machine Firefox then use Webdriver to start it so that the Firefox installed on the machine can be directly used by the plug-in is not required in the configuration profile:
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println ("Start Firefox browser ...");
System.setproperty ("Webdriver.firefox.bin", "C:/Program Files (x86)/mozilla Firefox/firefox.exe");
profilesini pi = new Profilesini ();
Firefoxprofile profile = Pi.getprofile ("Default");
Webdriver Driver = new Firefoxdriver (profile);
Driver.get ("http://www.baidu.com");
System.out.println ("Start Firefox browser succeed ...");
}
Start IE Browser.
PS: in addition to Firefox has been brought out of other browsers are required from Selenium official website http://docs.seleniumhq.org/download/ Download the respective driver.
1 start the local IE Browser.
System.setproperty ("Webdriver.ie.driver",
"E:\\selenium\\iedriverserver.exe");//IEDriverServer.exe Local Path
Desiredcapabilities iecapabilities = Desiredcapabilities.internetexplorer ();
Iecapabilities.setcapability (Internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains, true);
Webdriver Driver = new Internetexplorerdriver (iecapabilities);
Driver.get ("http://www.baidu.com");
Start Chrome Browser.
1 launch the local chrome Browser.
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.setproperty ("Webdriver.chrome.driver",
"E:\\selenium\\chromedriver.exe");//Chromedriver.exe Local Path
Webdriver Driver = new Chromedriver ();
Driver.get ("http://www.baidu.com");
Driver.findelement (By.id ("kw")). SendKeys (Keys.chord (Keys.shift, "Webdriver"));
Driver.findelement (By.id ("su")). Click ();
Driver.close ();
}
Webdriver Summary-How to start different browsers