How to Run Selenium in Chrome Browser?

Selenium is a powerful tool for automating web browsers. It is widely used for testing web applications and can control browsers like Chrome, Firefox, and Safari. 

This blog will walk you through the steps to run Selenium in the Chrome browser using Java. We will cover the installation of necessary software, writing your first test script, and running it. 

If you are looking for software training in Bangalore, this guide will be especially beneficial as it provides a comprehensive introduction to Selenium testing.

What is Selenium?

Selenium is an open-source tool used for automating web browsers. It is commonly used for web testing but can also be used for web scraping and automating repetitive tasks on websites. Selenium supports multiple programming languages, including Python, Java, C#, and Ruby. In this guide, we will use Java for writing our scripts.

Prerequisites

Before we start, make sure you have the following installed on your system:

  1. Java Development Kit (JDK): You can download the JDK from oracle.com. Make sure to install the latest version.
  2. Eclipse IDE: Download and install Eclipse IDE from eclipse.org.
  3. Google Chrome: Download and install the latest version of Google Chrome from google.com/chrome.
  4. ChromeDriver: This is a separate executable that Selenium WebDriver uses to control Chrome. You can download it from chromedriver.chromium.org.

Step 1: Install Java and Eclipse

Before we start, make sure you have the following installed on your system:

  1. Java Development Kit (JDK): You can download the JDK from oracle.com. Make sure to install the latest version.
  2. Eclipse IDE: Download and install Eclipse IDE from eclipse.org.
  3. Google Chrome: Download and install the latest version of Google Chrome from google.com/chrome.
  4. ChromeDriver: This is a separate executable that Selenium WebDriver uses to control Chrome. You can download it from chromedriver.chromium.org.

Step 1: Install Java and Eclipse

First, you need to ensure Java is installed. Open your terminal or command prompt and type:

bash

java -version

If Java is installed, you will see the version number. If not, install it from the Oracle website.

Next, download and install Eclipse IDE from the Eclipse website. Eclipse is an integrated development environment (IDE) used for Java development.

Step 2: Download ChromeDriver

ChromeDriver is a separate executable that Selenium uses to control Chrome. Download the version of ChromeDriver that matches your installed version of Chrome. You can find it on the ChromeDriver download page.

After downloading, extract the executable and place it in a directory that is included in your system’s PATH. You can add the directory to your PATH by following the instructions for your operating system.

Step 3: Setup Selenium in Eclipse

Open Eclipse and create a new Java project:

  1. Go to File > New > Java Project.
  2. Name your project (e.g., “SeleniumProject”).
  3. Click Finish.

Next, you need to add the Selenium library to your project:

  1. Right-click on your project and go to Build Path > Configure Build Path.
  2. In the Libraries tab, click Add External JARs.
  3. Download the Selenium Java Client Driver from the Selenium website.
  4. Add the JAR files from the downloaded Selenium zip file.

Step 4: Write Your First Selenium Script

Now that we have all the prerequisites installed, let’s write our first Selenium script. Open Eclipse, create a new Java class, and name it TestSelenium.

Here’s a simple script to open the Google homepage in Chrome:

java

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class TestSelenium {

    public static void main(String[] args) {

        // Set the path for the ChromeDriver

        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);

        // Create a new instance of the Chrome driver

        WebDriver driver = new ChromeDriver();

        // Open the Google homepage

        driver.get(“https://www.google.com”);

        // Close the browser

        driver.quit();

    }

 

}

Make sure to replace ‘/path/to/chromedriver’ with the actual path to your ChromeDriver executable.

Step 5: Run Your Selenium Script

To run your script, right-click on your Java file in Eclipse and select Run As > Java Application.

You should see a new Chrome window open, navigate to the Google homepage, and then close.

Step 6: Performing Actions in Selenium

Opening a webpage is just the beginning. Selenium allows you to perform various actions on web pages, such as clicking buttons, entering text, and extracting information. Let’s modify our script to perform a Google search.

Java code

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class TestSelenium {

    public static void main(String[] args) {

        // Set the path for the ChromeDriver

        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);

 

        // Create a new instance of the Chrome driver

        WebDriver driver = new ChromeDriver();

        // Open the Google homepage

        driver.get(“https://www.google.com”);

        // Find the search box using its name attribute value

        WebElement searchBox = driver.findElement(By.name(“q”));

        // Enter text into the search box

        searchBox.sendKeys(“Selenium WebDriver”);

        // Simulate pressing the Enter key

        searchBox.sendKeys(Keys.RETURN);

        // Close the browser

        driver.quit();

    }

 

}

 

In this script, we first navigate to the Google homepage. We then locate the search box using its name attribute (which is q), enter the text “Selenium WebDriver” into the search box, and simulate pressing the Enter key to perform the search.

Step 7: Waiting for Elements to Load

Web pages can take time to load, and elements may not be immediately available. Selenium provides various ways to wait for elements. One common approach is to use implicitly_wait, which tells Selenium to wait for a specified amount of time before throwing an exception if an element is not found.

Here’s how you can add an implicit wait to your script:

java code

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

import java.io.File;

import java.io.IOException;

import org.apache.commons.io.FileUtils;

 

public class TestSelenium {

    public static void main(String[] args) {

        // Set the path for the ChromeDriver

        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);

 

        // Create a new instance of the Chrome driver

        WebDriver driver = new ChromeDriver();

 

        // Implicitly wait for elements to be ready before attempting interactions

        driver.manage().timeouts().implicitlyWait(10, java.util.concurrent.TimeUnit.SECONDS);

 

        // Open the Google homepage

        driver.get(“https://www.google.com”);

 

        // Find the search box using its name attribute value

        WebElement searchBox = driver.findElement(By.name(“q”));

 

        // Enter text into the search box

        searchBox.sendKeys(“Selenium WebDriver”);

 

        // Simulate pressing the Enter key

        searchBox.sendKeys(Keys.RETURN);

 

        // Take a screenshot of the results page

        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        try {

            FileUtils.copyFile(screenshot, new File(“google_search_results.png”));

        } catch (IOException e) {

            e.printStackTrace();

        }

 

        // Close the browser

        driver.quit();

    }

 

}

 

 

With this implicit wait, Selenium will wait up to 10 seconds for elements to become available before proceeding.

Step 9: Handling Different Elements

Web pages contain various types of elements such as buttons, links, and dropdown menus. Selenium provides methods to interact with all these elements. Let’s look at some examples.

Clicking a Button

To click a button, you can use the click method. Here’s an example of how to click the “Google Search” button:

Java code

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class TestSelenium {

    public static void main(String[] args) {

        // Set the path for the ChromeDriver

        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);

 

        // Create a new instance of the Chrome driver

        WebDriver driver = new ChromeDriver();

 

        // Implicitly wait for elements to be ready before attempting interactions

        driver.manage().timeouts().implicitlyWait(10, java.util.concurrent.TimeUnit.SECONDS);

 

        // Open the Google homepage

        driver.get(“https://www.google.com”);

 

        // Find the search box using its name attribute value

        WebElement searchBox = driver.findElement(By.name(“q”));

 

        // Enter text into the search box

        searchBox.sendKeys(“Selenium WebDriver”);

 

        // Find the Google Search button using its name attribute value and click it

        WebElement searchButton = driver.findElement(By.name(“btnK”));

        searchButton.click();

 

        // Close the browser

        driver.quit();

    }

 

}

 

 

Selecting from a Dropdown Menu

To select an option from a dropdown menu, you can use the Select class from selenium.webdriver.support.ui. Here’s an example:

java

Copy code

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.support.ui.Select;

 

public class TestSelenium {

    public static void main(String[] args) {

        // Set the path for the ChromeDriver

        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);

 

        // Create a new instance of the Chrome driver

        WebDriver driver = new ChromeDriver();

 

        // Implicitly wait for elements to be ready before attempting interactions

        driver.manage().timeouts().implicitlyWait(10, java.util.concurrent.TimeUnit.SECONDS);

 

        // Open a sample dropdown menu page

        driver.get(“https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html”);

 

        // Find the dropdown menu by its id and select an option by its visible text

        Select select = new Select(driver.findElement(By.id(“select-demo”)));

        select.selectByVisibleText(“Friday”);

 

        // Close the browser

        driver.quit();

    }

 

}

 

Step 10: Closing the Browser

It is important to close the browser after your tests to free up resources. You can close the browser using the quit method, which shuts down the WebDriver instance and closes the browser window.

Java code

driver.quit();

Alternatively, you can use the close method to close the current window, but if there are multiple windows open, only the current one will be closed.

Java code

driver.close();

Conclusion

Running Selenium in the Chrome browser with Java is a straightforward process once you have the necessary tools installed. By following the steps outlined in this guide, you can automate various tasks on web pages, perform web testing, and interact with different web elements. 

Whether you are testing a web application or automating a repetitive task, Selenium provides a robust and flexible framework for browser automation. 

If you are interested in further exploring this tool, consider enrolling in Selenium Training in Bangalore. Happy coding!

Similar Posts

Leave a Reply

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