How to Open Chrome Browser Using Selenium in Java?
Last Updated :
28 Mar, 2023
Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS, and its scripts are written in various languages i.e Java, Python, JavaScript, C#, etc. We will be working with Java. In this article, let us consider a test case in which we will try to automate the following scenarios in the Google Chrome browser.
For invoking the chrome browser, we need the Eclipse IDE, Selenium Grid(version 4), and Chrome web Driver.
Installation
- Eclipse IDE: Before downloading also make sure that your device has Java JDK. If you don't have, install Java refer to this: How to Download and Install Java for 64 bit machine?. And install Eclipse IDE by referring to this article Eclipse IDE for Java Developers.
- Selenium: Download the Selenium latest stable version here.
- Web Driver: Web drivers is a package to interact with a web browser. It interacts with the web browser or a remote web server through a wire protocol which is common to all. Download Chrome Driver according to your Chrome Version here.
Step by Step Implementation
Step 1:
Open the Eclipse IDE and create a new Java project. Right-click on the "src" folder and create a new Class File from New > Class. Give the Class name and click on the "Finish" button.
Step 2:
Add Selenium JAR file into the Java Project. Right-click on Class name and Select "Build Path" and select > configure build path

Then select Libraries > Classpath > and Click "Add External JAR's", now add the Selenium Jar and click "Apply and Finish"

Note: It's always recommended to use Maven Project instead of downloading the JARs and configuring them manually. Maven Project can automatically install all the required JARs in form of Dependencies and configure them in build path. Later if any new version need to used, we can simply update the version in pom.xml and the new JARs will be installed and configured automatically. You can learn more here.
Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GFG_Chrome {
public static void main(String args[])
{
System.setProperty(
"webdriver.chrome.driver",
"C:\\Users\\ADMIN\\Documents\\chromedriver.exe");
// Instantiate a ChromeDriver class.
WebDriver driver = new ChromeDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.geeksforgeeks.org/");
}
}
Studying the code
The above script is for opening Google Chrome Browser and navigating to geeksforgeeks website. So let’s see how it works:
- Set a system property "webdriver.chrome.driver" to the path of your ChromeDriver.exe file and instantiate a ChromeDriver class: System.setProperty("webdriver.chrome.driver","chromedriver location");
- Maximize the window: driver.manage().window().maximize();
- To open the URL: driver.get("URL link")
Note: Please note that with Selenium version 4.6.0, SeleniumManager has been introduced. It's still in beta version, but with SeleniumManager there is no need of external driver to launch Chrome, Edge or Firefox. External Driver always works for a specific version of the browser. As the browser version keeps updating automatically in most of the organisation, it was very difficult to keep updating the driver manually from time to time. With SeleniumManager that dependency has been removed. You can find more details on here.
Refer this video if any doubts
Similar Reads
How to Open Microsoft Edge Browser using Selenium in Java? Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS and its scripts are written in various languages i.e Java, Python, JavaScript, C#, e
3 min read
How to Open a Browser in Headless Mode in Selenium using Java? Headless testing has become a significant trend in modern web automation, offering numerous advantages for developers and testers. In Selenium, running tests in headless mode allows browsers to operate without the graphical user interface (GUI). This article delves into the concept of headless brows
6 min read
How to Run Opera Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. Th
3 min read
How to Run Gecko Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. It consists of three parts: Selenium IDE, Selenium WebDriver, and Selenium Grid. Selenium WebDriver is the most important. Using WebDriver, online website testing can be done. There are three main WebDriver implementations:ChromeD
5 min read
How to Handle Alert in Selenium using Java? Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
5 min read
How to Run Safari Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. Selenium consists of three parts: Selenium IDE, Selenium Webdriver, and Selenium Grid. Among these, Selenium Webdriver is the most important one. Using Webdriver, online website testing can be done. There are three main Webdrivers
4 min read