Getting Started with Selenium 4

Set up your system for Selenium 4 automation. Install Java/Python, understand how WebDriver works, and configure your first Selenium project.

7 min read

Selenium supports various programming languages like Java, Python, C#, Perl, Ruby, etc. Java & Python are one of the most widely used languages for Selenium.

In this tutorial, we will set up everything you need:

  • Installing programming language (Java/Python)
  • Understanding how WebDriver works (Yes, this is important!)
  • Setting up Selenium dependencies
  • Understanding browser drivers

Installing Java

If Java is already installed in your system, you may skip this section.

How would I get to know if Java is already installed in my system?

Open Command Prompt or Terminal. Type java -version and press Enter. If Java is pre-installed it will display you the Java version. You need Java 11 or higher for Selenium 4.

java -version

Let's follow these steps to download and install Java –

  • Go to Adoptium JDK Downloads (recommended) or Oracle JDK
  • Download JDK 17 or later (LTS version recommended)
  • Run the installer and follow on-screen instructions
  • Make sure to check "Add to PATH" during installation

Almost done? I thought installing Java is completely over.

Not yet dude. We need to verify Java is correctly configured.

Let's verify that –

Run following command in your terminal:

java -version

It should display the installed version like openjdk version "17.0.x".

Also run:

javac -version

If both commands show version numbers, you're golden!

Setting Up Selenium Dependencies

We'll use Maven to manage our Selenium dependencies. Trust me, it's way easier than manually downloading JAR files like the old days.

What the heck is Maven?

Well, think of Maven as a smart assistant that downloads all the libraries your project needs. You just tell it what you want in a file called pom.xml, and it fetches everything for you. No more hunting for JAR files on the internet!

Create a new Maven project:

Using IntelliJ IDEA (Recommended):

  • Open IntelliJ IDEA
  • Click "New Project"
  • Select "Maven" and click Next
  • Enter your project name (e.g., selenium-tutorial)
  • Click Finish

Using Eclipse:

  • Go to File β†’ New β†’ Maven Project
  • Check "Create a simple project" and click Next
  • Enter Group Id: com.yourname and Artifact Id: selenium-tutorial
  • Click Finish

Add Selenium dependency to your pom.xml:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.18.1</version>
    </dependency>
</dependencies>

That's it! Maven will automatically download Selenium and all its dependencies. No more manual JAR downloads!

Understanding How WebDriver Works

Browser drivers??? What they are? Why do I need them?

This is a very important question. Sadly, very few ask it and unfortunately, most of the selenium tutorials don't cover it. They just make you install things without explaining why.

Before answering those what and why I would first like to explain how WebDriver works. It's a bit technical. If it makes you more confused, just read it a couple of times and you'll get it with time.

All WebDriver language bindings (selenium-java, selenium for Python, etc.) use a special protocol to communicate with the browser – WebDriver Protocol (formerly known as JSON Wire Protocol).

This protocol is nothing but a RESTful web service. You write a WebDriver command by following the APIs provided by that particular WebDriver implementation (e.g. selenium-java client). Each such command is mapped to one or more REST calls to the server.

Server??? From where did it drop off. I guess you were talking about browser driver.

Well, yes. When the browser driver is executed, it actually creates an HTTP server that listens at a port for calls made by clients that use WebDriver Protocol. In our case, the client is the selenium library that we installed. So the language bindings (e.g. selenium-java client), maps your Selenium code to an HTTP request, hit the server created by the browser driver with that HTTP request. And finally, the server communicates with the browser.

In order to run your test against a specific browser, you need the driver for that browser. The browser drivers create a server when executed that act as a bridge between "browser" and the "client i.e WebDriver language bindings."

The Good News: Selenium Manager!

Wait, so I need to download ChromeDriver, GeckoDriver, EdgeDriver separately?

Here's where Selenium 4.6+ makes our life SO much easier! Selenium Manager is built right into Selenium and automatically downloads the correct browser driver for you.

In the old days (Selenium 3 and earlier), you had to:

  1. Check your Chrome version
  2. Find the matching ChromeDriver version
  3. Download it manually
  4. Set the path in your code using System.setProperty
  5. Repeat whenever Chrome auto-updates (nightmare!)

Now? Just make sure you have Chrome (or Firefox, Edge) installed on your system. Selenium Manager handles the rest automatically. No more version mismatch headaches!

One more setup and I swear I am gonna unsubscribe this tutorial.

No more setup dude. Cheers, this was the last one. By the way, this tutorial is free, so unsubscribing it won't hit me hard πŸ™‚

Let us get our hands dirty by writing our first ever Selenium scripts in the Next Tutorial.