Selenium Webdriver Commands

Wants to learn in depth about the various selenium webdriver command stata are supported by various browsers. If yes, please go ahead with this blog post, we are going to discuss how different selenium webdriver commands are used to launch various browsers in depth. First we will explore the basic browsing launching command then after explode the code snippet for launching the selenium webdriver commands in different browsers like safari, firefox, Chrome and Internet Explorer, etc.

Basic understanding of the browser launching command:

Selenium comes with different types of web drivers for different browsers. For example, firefox browser comes with Firefox Driver, Internet Explorer comes with InternetExplorerDriver and Google Chrome comes with ChroemDriver, etc.

Selenium WebDriver commands to Launch browsers:

As you all are aware of, the Selenium WebDriver calls native methods of different browsers in order to invoke the automating scripting. So we have the different web drivers for different browsers like FirefoxDriver for Firefox browser, InternetExplorerDriver for IE, etc.

Now let us consider the example of firefox browser.The command looks like as below:

WebDriver driver=new FirefoxDriver();

The above mentioned command is the java implementation of launching a browser in selenium. 

In the above command WebDriver acts as an interface and driver acts as an reference variable for the type WebDriver, that are instantiated for the FireFoxDriver class.

Now let us consider the below command and see how different it is from the above mentioned command.

WebDriver driver=new WebDriver();

As we know that an interface is a set of variables and methods without any body implementation. Here we cannot instantiate objects from interfaces, and the above command shows the compile time error.

So for instantiating the driver objects , we need classes like FirefoxDriver or ChromeDriver which have implemented the WebDriver interface.

Become a master of Selenium by going through this HKR Selenium Training!

Selenium Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Launching firefox browser:

Firefox serves as the most prominent browser in doing automation and the following steps are required to launch the firefox browser.

  • Download geckodriver.exe from GeckoDriver Github Release Page. Make sure to download the right driver file based on your platform and OS version.
  • Set the System Property for “webdriver.gecko.driver” with the geckodriver.exe path – System.setProperty(“webdriver.gecko.driver”,”geckodriver.exe path”);

Code Snippet to launch the firefox browser

package testPackage;

import org.openqa.selenium.WebDriver;

public class FirefoxBrowserLaunchDemo {

    public static void main(String[] args) {

                //Creating a driver object referencing WebDriver interface

        WebDriver driver;

             //Setting webdriver.gecko.driver property

        System.setProperty("webdriver.gecko.driver", "{path to geckodriver}\\geckodriver.exe");

         //Instantiating driver object and launching browser

        driver = new FirefoxDriver();

        //Using get() method to open a webpage

        driver.get("https://hkrtrainings.com/");

        //Closing the browser

        driver.quit();

  }

 }

In order to run the test just do right-click on Test.java file on the Package Explorer section, hover over “Run As” and select “Java Application”. Firefox browser will launch and open hkrtrainings.com

If you want to Explore more about Selenium? then read our updated article - Selenium Tutorial

Launching Chrome browser:

For running the Chrome browser in Selenium, we need to set the webdriver.chrome.driver system property to point to a chrome driver executable file-

Download the latest ChromeDriver binary from Chromium.org download page and place the executable on your local machine.
Set the webdriver.chrome.driver property to the chromeDriver.exe’s location as-System.setProperty(“webdriver.chrome.driver”, “chromeDriver.exe path”);
Code snippet to launch the Chrome browser

public class ChromeBrowserLaunchDemo {

    public static void main(String[] args) {

         //Creating a driver object referencing WebDriver interface

        WebDriver driver;

                //Setting the webdriver.chrome.driver property to its executable's location

        System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");

        //Instantiating driver object

        driver = new ChromeDriver();

                //Using get() method to open a webpage

        driver.get("https://artoftesting.com");

                //Closing the browser

        driver.quit();

     }

 }

Launching IE Browser:

InternetExplorer driver also requires setting up the “webdriver.ie.driver” property with the location of IEDriverServer.exe. The IEDriverServer.exe can be downloaded from here. 

Following code, snippet can be used to launch IE browser

public class IEBrowserLaunchDemo {

    public static void main(String[] args) {

           //Creating a driver object referencing WebDriver interface

        WebDriver driver;

          //Setting the webdriver.ie.driver property to its executable's location

        System.setProperty("webdriver.ie.driver", "/lib/IEDriverServer/IEDriverServer.exe");

        //Instantiating driver object

        driver = new InternetExplorerDriver();

                //Using get() method to open a webpage

        driver.get("https://artoftesting.com");

               //Closing the browser

        driver.quit();

     }

 }

Subscribe to our youtube channel to get new updates..!

Launching Safari Browser:

Safari browser canan be directly launched by instantiating with SafariDriver. The following code snippet can be used to launch the Safari browser-

public class SafariBrowserLaunchDemo {

    public static void main(String[] args) {

          //Creating a driver object referencing WebDriver interface

        WebDriver driver;

       //Instantiating driver object with SafariDriver

        driver = new SafariDriver();

         //Using get() method to open a webpage

        driver.get("https://artoftesting.com");

          //Closing the browser

        driver.quit();

    }


 }

Selenium Certification Training

Weekday / Weekend Batches

Conclusion

In the blog content, I had covered how selenium webdriver commands used to launch different browsers. If you had any doubts regarding it please drop the message in the comments section. For more information regarding selenium simply click here for complete Selenium Tutorial.

Find our upcoming Selenium Certification Training Online Classes

  • Batch starts on 28th Oct 2021, Weekday batch

  • Batch starts on 1st Nov 2021, Weekday batch

  • Batch starts on 5th Nov 2021, Fast Track batch

Global Promotional Image
 

Categories

Request for more information

Srivalli
Srivalli
Research Analyst
As a senior technical content writer for HRK tainings, srivalli patchava has a greater understanding of today's data-driven environment, which includes key aspects of data management and IT organizations. She manages the task of creating great content in the areas of software testing, DevOps, Robotic process automation. Connects with her on Linkedin and Twitter.