The Wayback Machine - https://web.archive.org/web/20240620013918/https://www.geeksforgeeks.org/how-to-open-chrome-browser-using-selenium-in-java/
Open In App

How to Open Chrome Browser Using Selenium in Java?

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. 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”); 
  2. Maximize the window: driver.manage().window().maximize();  
  3. 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 Create a Selenium Maven Project with Eclipse to Open Chrome Browser?
Maven is a Yiddish word meaning accumulator of knowledge, Maven is a build automation tool. It is a software project management and comprehension tool that can manage the project’s build, reporting, and documentation. Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is that it supports all browsers
3 min read
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#, etc. We will be working with Java. In this article,
3 min read
How to Open a Browser in Headless Mode in Selenium using Java?
Selenium is an open-source web automation tool that supports many user actions to perform testing in the web browser. Why Headless Browser? Executing the automation code to run on a web browser is used to perform UI testing and functional testing, But sometimes while we run our automation scripts on the browser the page takes time to load all the U
2 min read
Java Program to Open Input URL in System Default Browser in Windows
URL is the URL you want to display in the default Web browser. For example, wire http://www.site.com/report.html to this input to display the HTML file report.html on the Web Server www.site.com in the browser. Our problem is to write a java program to open Input URL in System Default Browser in Windows. To open any URL we use different predefine f
2 min read
How to Install Google Chrome Browser on MacOS?
Google Chrome is a web browser. Google Chrome can be run on various platforms like macOS, windows, android, etc. It was made for the purpose of browsing web pages in an easy way. It was first launched in 2008. The development of google chrome goes to Google Inc. It is open-source software and it have many features like incognito mode, bookmarks, us
2 min read
How to Install Google Chrome Browser on Windows?
Google Chrome is a web browser that can be used on any platform whether desktop, mobiles, or tablet. It is one of the highest used web browsers and holds nearly 68 percent of the market of web browsers. It can be used on any operating system like Windows, Linux, Mac, etc. It is developed using a number of programming languages like JavaScript, Java
2 min read
How to Cast From a Chrome Browser?
Casting is a screen mirroring feature that allows casting to view your device screen on the TV using the same Wi-Fi network connection. you can use Chromecast to display it on your PC tab on your TV screen. Most of the content will be displayed, but sometimes the content will not display due to security features .you can cast your display to variou
2 min read
How to Open a URL in Android's Web Browser in an Android Application?
It might be essential to launch a browser separately while using an Android application to access a URL. Any action made on Views using Action Listeners has the potential to initiate the task of accessing a URL. In this article, we will learn about How we can Open a URL in Android's Web Browser from an Android Application. For this article, we will
2 min read
How to Handle Static Web Tables using Selenium WebDriver using Java?
Web Tables are used to organize similar types of content on the web page, Web tables are groups of elements that are logically stored in a row and column format. Web table has various HTML tags like., table, th, tr, td. Let's understand these tags a bit more: <table> - It defines a table<th> - It defines a header cell<tr> - It def
3 min read
How to Select Multiple Elements using Actions Class in Selenium using Java?
Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a web page that has file upload or other functionality which requires selecting multiple elements, to perform the multiple select actions the selenium provides a class called Actions. The Action class provides the method for Keyb
2 min read