Select Page
Automation Testing

An All-Inclusive Guide to Run Selenium WebDriver in Multiple Browsers

Looking to run Selenium WebDriver in Multiple Browsers like Chrome, Safari, Firefox, Edge, and Opera? You're at the right place.

An All-Inclusive Guide to Run Selenium WebDriver in Multiple Browsers Blog

When you are testing a web-based application, it is pivotal to ensure that they work without any issues in the browsers that are predominantly used by the world. Selenium WebDriver is a great tool that allows QA professionals to automate their test cases in their desired browser by using its library and a language-specific framework. As a leading QA company, we believe the ability to run Selenium WebDriver in different browsers is a basic skill every automation tester must possess. So we have written this blog to provide a comprehensive guide to the ones looking to learn how to run Selenium WebDriver in browsers like Chrome, Firefox, Safari, Opera, and Edge. So let’s get started with a few basics of Selenium and move forward from there.

Selenium

Selenium is a popular test automation tool that was developed by Jason Huggins in 2004 at Thought Works. It is an open-source automated testing framework for web applications that works with different languages such as JavaScript (Node.js), Python, Ruby, Java, or C#.

Different Types of Selenium:
  • Selenium IDE
  • Selenium RC
  • Selenium WebDriver
  • Selenium Grid

Selenium WebDriver:

We have already established how Selenium WebDriver can be instrumental in testing a web application across different browsers. But before you can get started with your browser automation, you should make sure to download the browser-specific drivers. Also ensure that the driver is compatible with your OS (Windows, Mac, or Linux).

Though we will be guiding you to run Selenium WebDriver in Safari that runs only on macOS, we will be focusing on how you can run Selenium WebDriver in the other browsers on Windows.

Different types of WebDriver:

1. Firefox

2. Opera

3. Chrome

4. Edge

5. Safari

Both developers and QAs have the liberty to choose the programming language of their choice all thanks to the wide range of language bindings that the Selenium developers have developed.

Now, let’s see a list of software that you will have to download onto your system to successfully automate your browsers. Make sure you download the most recent and stable releases.

1. Selenium Java Stable 4.0.0

2. JDK

3. IntelliJ IDEA

Environment setup:

Setting up an environment for testing is one of the very first actions we would be doing. So let’s see how to do that in Java.

  • Open ‘Edit the System Environment Variables’ options by searching for it in the Search Box.
  • Click on ‘Environment Variables’ -> Click the ‘New’ Button
  • Enter the variable name as ‘CLASSPATH’, and enter the following variable value ‘C:\ProgramFiles\Java\jdk-17.0.1\lib\*.jar’.
  • Click the ‘New’ button again.
  • Enter Variable name ‘JAVA_HOME’ and Variable value ‘C:\Program Files\Java\jdk-17.0.1’.

Once it has been downloaded, verify it in your system without fail. You can do that by opening command prompt and typing java–version and clicking Enter. If there are no issues, you will see the details of the Java & JDK versions.

IntelliJ IDEA

  • Open IntelliJ. Click File -> New -> Project -> Click Java -> Select Project SDK -> Next -> Select the “Checkbox” to Create project from template, Click ‘Next’ -> Give a name for the Project, and click on Finish.
  • Click on ‘File’ -> Project Structure -> Platform Settings ->SDKs to Add Classpath ‘+’.
  • Open the Selenium file and navigate to the Lib folder, select ‘All Jar Files’ and click on ‘Apply’.
  • Make you sure downloading Selenium Webdriver in any compatible browser on your windows . Click the URL and go to navigate the Platforms Supported by Selenium and click the browser then you can the webdriver then download the documentation.
  • Must you verify the browser version of the webdriver is here , then you can download and use it .
The syntax for the WebDriver
//System.setProperty ("webdriver.chrome.driver"," Enter the driver path with driver name.exe "); //

Keywords for Selenium:

In order to understand the sample codes that we have written, you have to know a few basic keywords that are used to perform certain actions in the automation process. We have just mentioned a few basic keywords, if you are looking to get a better understanding, make sure to check out our blog that will help you with it.

Action:

We can use these keywords to open a particular website, reload it, close it, and such other actions.

driver.get ("URL");
driver.navigate ().to ("URL");
driver.navigate ().refresh ();
driver.navigate ().forward ();
driver.navigate ().back ();
driver. Close ();
Locators:

These keywords are used to locate the elements using Selenium. Each keyword uses a different method and it can be easily understood by their names.

driver.findElement(By.id(""));
driver.findElement(By.name(""));
driver.findElement(By.xpath(""));
driver.findElement(By.cssSelector(""));
driver.findElement(By.linkText(""));
driver.findElement(By.partialLinkText(""));

Code to Run Selenium WebDriver in Multiple Browsers:

We have written sample programs that will help you understand the changes you’ll have to make when using each browser. So let’s take a look at each of them one by one.

Sample Program for Chrome:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {

   public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver","D:\\Webdriver\\chromedriver_win32 (2)\\chromedriver.exe");
       WebDriver driver=new ChromeDriver();
       driver.get("https://www.snapdeal.com/");
       driver.manage().window().maximize();
       WebElement element =driver.findElement(By.cssSelector("ul[class='nav smallNav']>li[navindex='4']"));
       Actions action  = new Actions(driver);
       action.click(element).perform();
       WebElement element2 =driver.findElement(By.xpath("//span[text()='Keyboards']"));
       Actions actions = new Actions(driver);
       actions.doubleClick(element2).perform();
       driver.findElement(By.id("searchWithinSearch")).sendKeys("logitech");
       driver.findElement(By.id("swsIco")).click();
       driver.navigate().to("https://www.snapdeal.com/product/logitech-k120-black-usb-wired/636481286288");
       driver.findElement(By.id("buy-button-id")).click();
       
       driver.close();
   }
}

Sample Program for Firefox:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.concurrent.TimeUnit;

public class Main {

   public static void main(String[] args) {
       System.setProperty("webdriver.gecko.driver","D:\\Webdriver\\geckodriver-v0.29.1-win64\\geckodriver.exe");
       WebDriver driver=new FirefoxDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.MICROSECONDS);
       driver.manage().window().maximize();
       driver.get("http://www.leafground.com/pages/Dropdown.html");
       Select  Select_training_program_using_Index1 = new Select( driver.findElement(By.xpath("//select[@id='dropdown1']")));
       Select_training_program_using_Index1.selectByIndex(1);

       Select  Select_training_program_using_Index2 = new Select( driver.findElement(By.xpath("//select[@name='dropdown2']")));
       Select_training_program_using_Index2.selectByValue("2");

       Select  Select_training_program_using_Index3 = new Select( driver.findElement(By.xpath("//select[@id='dropdown3']")));
       Select_training_program_using_Index3.selectByVisibleText("UFT/QTP");

       Select Get_the_number_of_dropdown_options = new Select( driver.findElement(By.xpath("//select[@class='dropdown']")));
       Get_the_number_of_dropdown_options.getWrappedElement().sendKeys("Loadrunner");

   }
}


Sample Program for Microsoft Edge:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class Main{

   public static void main(String[] args) {
       System.setProperty("webdriver.edge.driver","C:\\Users\\Admin\\Downloads\\edgedriver_win32\\msedgedriver.exe");
       WebDriver driver= new EdgeDriver();
       driver.manage ().window().maximize();
driver.manage().deleteAllCookies();
driver.get("https://facebook.com ");
driver.findElement(By.id("email")).sendKeys("7418894451");
driver.findElement(By.name("pass")).sendKeys("99664475");
driver.findElement(By.xpath("//button[@name='login']")).click();
driver.close();

   }
}

Sample Program for Opera:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.opera.OperaDriver;
import java.util.concurrent.TimeUnit;

class Main{

   public static void main(String[] args) {
       System.setProperty("webdriver.opera.driver","C:\\Users\\Admin\\Downloads\\operadriver_win64 (1)\\operadriver_win64\\operadriver.exe");
       WebDriver driver=new OperaDriver();
       driver.manage().window().maximize();
       driver.manage().deleteAllCookies();
       driver.get("https://www.google.co.in");
       driver.manage().deleteAllCookies();
       driver.navigate().to("https://codoid.com");
       driver.manage().window().fullscreen();
       driver.navigate().back();
       driver.navigate().refresh();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.MICROSECONDS);
       driver.navigate().forward();
       driver.manage().window().fullscreen();
       driver.close();
   }}

Run the WebDriver in the Safari browser

So as promised, now we are going to explore the two processes you’ll need to do to run Selenium WebDriver in Safari. Following this, we have a test script for the Safari driver to run. It is worth noting that we’d have to run an automation test in the Safari browser to enable this option.

Process 1

Step 1: Open the Safari browser and navigate to the Preferences.

Step 2: Enable the ‘Show Develop Menu’ on the Menu bar.

Step 3: Now you’ll see the new Develop option popup on your safari menu bar.

Step 4: Click on ‘Allow remote automation’.

Step 5: Once that is done, you’ll be able to run the web driver successfully.

Syntax:

Webdriver driver = new Safari Webdriver ();

//Since this Safari WebDriver there is no need to set the path of the driver for the respective browser, instead you would have to do it for the system.

For example,

System.setProperty("webdriver.safari.driver","C:\\Admin\\safaridriver(1)\\safaridriver.exe");

//Apple developed a Safari WebDriver that is compatible with all the safari browsers.

//If you can’t run a Safari WebDriver, you can activate the WebDriver by following Process 1.

Sample program for Safari:
package com.company;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class Main{

  public static void main(String[] args) {
       WebDriver driver=new SafariDriver();

        driver.get("https://codoid.com/ ");
driver.close();
   }}

Conclusion

We hope you found this blog informative as a majority of software testers use Selenium WebDriver to automate their tests in different browsers. As one of the prominent automation testing service providers in the arena, we ourselves have been ardent users of Selenium WebDriver to automate our tests.

Written By

Submit a Comment

Your email address will not be published. Required fields are marked *


When you are testing a web-based application, it is pivotal to ensure that they work without any issues in the browsers that are predominantly used by the world. Selenium WebDriver is a great tool that allows QA professionals to automate their test cases in their desired browser by using its library and a language-specific framework. As a leading QA company, we believe the ability to run Selenium WebDriver in different browsers is a basic skill every automation tester must possess. So we have written this blog to provide a comprehensive guide to the ones looking to learn how to run Selenium WebDriver in browsers like Chrome, Firefox, Safari, Opera, and Edge. So let’s get started with a few basics of Selenium and move forward from there.

Selenium

Selenium is a popular test automation tool that was developed by Jason Huggins in 2004 at Thought Works. It is an open-source automated testing framework for web applications that works with different languages such as JavaScript (Node.js), Python, Ruby, Java, or C#.

Different Types of Selenium:
  • Selenium IDE
  • Selenium RC
  • Selenium WebDriver
  • Selenium Grid

Selenium WebDriver:

We have already established how Selenium WebDriver can be instrumental in testing a web application across different browsers. But before you can get started with your browser automation, you should make sure to download the browser-specific drivers. Also ensure that the driver is compatible with your OS (Windows, Mac, or Linux).

Though we will be guiding you to run Selenium WebDriver in Safari that runs only on macOS, we will be focusing on how you can run Selenium WebDriver in the other browsers on Windows.

Different types of WebDriver:

1. Firefox

2. Opera

3. Chrome

4. Edge

5. Safari

Both developers and QAs have the liberty to choose the programming language of their choice all thanks to the wide range of language bindings that the Selenium developers have developed.

Now, let’s see a list of software that you will have to download onto your system to successfully automate your browsers. Make sure you download the most recent and stable releases.

1. Selenium Java Stable 4.0.0

2. JDK

3. IntelliJ IDEA

Environment setup:

Setting up an environment for testing is one of the very first actions we would be doing. So let’s see how to do that in Java.

  • Open ‘Edit the System Environment Variables’ options by searching for it in the Search Box.
  • Click on ‘Environment Variables’ -> Click the ‘New’ Button
  • Enter the variable name as ‘CLASSPATH’, and enter the following variable value ‘C:\ProgramFiles\Java\jdk-17.0.1\lib\*.jar’.
  • Click the ‘New’ button again.
  • Enter Variable name ‘JAVA_HOME’ and Variable value ‘C:\Program Files\Java\jdk-17.0.1’.

Once it has been downloaded, verify it in your system without fail. You can do that by opening command prompt and typing java–version and clicking Enter. If there are no issues, you will see the details of the Java & JDK versions.

IntelliJ IDEA

  • Open IntelliJ. Click File -> New -> Project -> Click Java -> Select Project SDK -> Next -> Select the “Checkbox” to Create project from template, Click ‘Next’ -> Give a name for the Project, and click on Finish.
  • Click on ‘File’ -> Project Structure -> Platform Settings ->SDKs to Add Classpath ‘+’.
  • Open the Selenium file and navigate to the Lib folder, select ‘All Jar Files’ and click on ‘Apply’.
  • Make you sure downloading Selenium Webdriver in any compatible browser on your windows . Click the URL and go to navigate the Platforms Supported by Selenium and click the browser then you can the webdriver then download the documentation.
  • Must you verify the browser version of the webdriver is here , then you can download and use it .
The syntax for the WebDriver
//System.setProperty ("webdriver.chrome.driver"," Enter the driver path with driver name.exe "); //

Keywords for Selenium:

In order to understand the sample codes that we have written, you have to know a few basic keywords that are used to perform certain actions in the automation process. We have just mentioned a few basic keywords, if you are looking to get a better understanding, make sure to check out our blog that will help you with it.

Action:

We can use these keywords to open a particular website, reload it, close it, and such other actions.

driver.get ("URL");
driver.navigate ().to ("URL");
driver.navigate ().refresh ();
driver.navigate ().forward ();
driver.navigate ().back ();
driver. Close ();
Locators:

These keywords are used to locate the elements using Selenium. Each keyword uses a different method and it can be easily understood by their names.

driver.findElement(By.id(""));
driver.findElement(By.name(""));
driver.findElement(By.xpath(""));
driver.findElement(By.cssSelector(""));
driver.findElement(By.linkText(""));
driver.findElement(By.partialLinkText(""));

Code to Run Selenium WebDriver in Multiple Browsers:

We have written sample programs that will help you understand the changes you’ll have to make when using each browser. So let’s take a look at each of them one by one.

Sample Program for Chrome:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {

   public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver","D:\\Webdriver\\chromedriver_win32 (2)\\chromedriver.exe");
       WebDriver driver=new ChromeDriver();
       driver.get("https://www.snapdeal.com/");
       driver.manage().window().maximize();
       WebElement element =driver.findElement(By.cssSelector("ul[class='nav smallNav']>li[navindex='4']"));
       Actions action  = new Actions(driver);
       action.click(element).perform();
       WebElement element2 =driver.findElement(By.xpath("//span[text()='Keyboards']"));
       Actions actions = new Actions(driver);
       actions.doubleClick(element2).perform();
       driver.findElement(By.id("searchWithinSearch")).sendKeys("logitech");
       driver.findElement(By.id("swsIco")).click();
       driver.navigate().to("https://www.snapdeal.com/product/logitech-k120-black-usb-wired/636481286288");
       driver.findElement(By.id("buy-button-id")).click();
       
       driver.close();
   }
}

Sample Program for Firefox:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.concurrent.TimeUnit;

public class Main {

   public static void main(String[] args) {
       System.setProperty("webdriver.gecko.driver","D:\\Webdriver\\geckodriver-v0.29.1-win64\\geckodriver.exe");
       WebDriver driver=new FirefoxDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.MICROSECONDS);
       driver.manage().window().maximize();
       driver.get("http://www.leafground.com/pages/Dropdown.html");
       Select  Select_training_program_using_Index1 = new Select( driver.findElement(By.xpath("//select[@id='dropdown1']")));
       Select_training_program_using_Index1.selectByIndex(1);

       Select  Select_training_program_using_Index2 = new Select( driver.findElement(By.xpath("//select[@name='dropdown2']")));
       Select_training_program_using_Index2.selectByValue("2");

       Select  Select_training_program_using_Index3 = new Select( driver.findElement(By.xpath("//select[@id='dropdown3']")));
       Select_training_program_using_Index3.selectByVisibleText("UFT/QTP");

       Select Get_the_number_of_dropdown_options = new Select( driver.findElement(By.xpath("//select[@class='dropdown']")));
       Get_the_number_of_dropdown_options.getWrappedElement().sendKeys("Loadrunner");

   }
}


Sample Program for Microsoft Edge:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class Main{

   public static void main(String[] args) {
       System.setProperty("webdriver.edge.driver","C:\\Users\\Admin\\Downloads\\edgedriver_win32\\msedgedriver.exe");
       WebDriver driver= new EdgeDriver();
       driver.manage ().window().maximize();
driver.manage().deleteAllCookies();
driver.get("https://facebook.com ");
driver.findElement(By.id("email")).sendKeys("7418894451");
driver.findElement(By.name("pass")).sendKeys("99664475");
driver.findElement(By.xpath("//button[@name='login']")).click();
driver.close();

   }
}

Sample Program for Opera:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.opera.OperaDriver;
import java.util.concurrent.TimeUnit;

class Main{

   public static void main(String[] args) {
       System.setProperty("webdriver.opera.driver","C:\\Users\\Admin\\Downloads\\operadriver_win64 (1)\\operadriver_win64\\operadriver.exe");
       WebDriver driver=new OperaDriver();
       driver.manage().window().maximize();
       driver.manage().deleteAllCookies();
       driver.get("https://www.google.co.in");
       driver.manage().deleteAllCookies();
       driver.navigate().to("https://codoid.com");
       driver.manage().window().fullscreen();
       driver.navigate().back();
       driver.navigate().refresh();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.MICROSECONDS);
       driver.navigate().forward();
       driver.manage().window().fullscreen();
       driver.close();
   }}

Run the WebDriver in the Safari browser

So as promised, now we are going to explore the two processes you’ll need to do to run Selenium WebDriver in Safari. Following this, we have a test script for the Safari driver to run. It is worth noting that we’d have to run an automation test in the Safari browser to enable this option.

Process 1

Step 1: Open the Safari browser and navigate to the Preferences.

Step 2: Enable the ‘Show Develop Menu’ on the Menu bar.

Step 3: Now you’ll see the new Develop option popup on your safari menu bar.

Step 4: Click on ‘Allow remote automation’.

Step 5: Once that is done, you’ll be able to run the web driver successfully.

Syntax:

Webdriver driver = new Safari Webdriver ();

//Since this Safari WebDriver there is no need to set the path of the driver for the respective browser, instead you would have to do it for the system.

For example,

System.setProperty("webdriver.safari.driver","C:\\Admin\\safaridriver(1)\\safaridriver.exe");

//Apple developed a Safari WebDriver that is compatible with all the safari browsers.

//If you can’t run a Safari WebDriver, you can activate the WebDriver by following Process 1.

Sample program for Safari:
package com.company;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class Main{

  public static void main(String[] args) {
       WebDriver driver=new SafariDriver();

        driver.get("https://codoid.com/ ");
driver.close();
   }}

Conclusion

We hope you found this blog informative as a majority of software testers use Selenium WebDriver to automate their tests in different browsers. As one of the prominent automation testing service providers in the arena, we ourselves have been ardent users of Selenium WebDriver to automate our tests.