Your First Selenium Script
Write and run your very first Selenium 4 script. Learn to launch a browser, navigate to a webpage, and interact with elements.
In the Previous Tutorial, we set up our system for Selenium 4. Now it's time for the fun part β let's write our very first Selenium script and watch the magic happen!
We'll automate a simple scenario:
- Open Chrome browser
- Navigate to a practice page
- Fill in a form field
- Click a button
- Close the browser
Ready? Let's go!
Create Your First Java Class
If you followed the previous tutorial, you should have a Maven project ready. Let's create a new Java class:
- In your project, right-click on
src/main/java - Select New β Class
- Name it
MyFirstSeleniumScript - Check "public static void main" and click Finish
Let's Write the Code β Step by Step
Step 1: Import Required Packages
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
Hold on⦠What are all these imports?
We need to import these packages to use WebDriver classes. If you're using an IDE like IntelliJ or Eclipse, it will prompt you to import them automatically.
What is a package, man?
Well⦠It is Java stuff. For now, you can think of Java package as a college library where there are sections for different book genres like Fiction, Technology etc. Each section consists of lots of books of that category. Similarly, a Java package is a placeholder for classes, interfaces, and methods.
Let me break down what we're importing:
WebDriverβ The main interface to control the browserWebElementβ Represents elements on the page (buttons, text fields, etc.)Byβ Helps us locate elements on the pageChromeDriverβ Implementation of WebDriver for Chrome browser
Step 2: Launch Chrome Browser
WebDriver driver = new ChromeDriver();
That's it? No System.setProperty or chromedriver path?
Exactly! In Selenium 4.6+, Selenium Manager handles everything automatically. It detects your Chrome version and downloads the matching ChromeDriver. How cool is that?
OK. Can you please tell me what that line of code is doing β WebDriver driver = new ChromeDriver();
We are declaring object reference of WebDriver Interface and making it point to object of ChromeDriver Class.
Would you please sound less technical?
Well, we are creating an object of ChromeDriver and asking WebDriver to hold it tightly. 'WebDriver' has appointed 'driver' to do this job. Once that line gets executed, it will launch Chrome browser.
I want to know more about Java Classes, Objects and Object References. In fact, I want to know more about Java.
I would recommend going through any tutorial on Java basics. There are very good video courses available at Udemy that teach Java from the very basics. For now, just know that driver is your handle to control the browser.
Step 3: Maximize Browser Window
driver.manage().window().maximize();
This makes the browser window full-screen. It's a good practice to maximize the window before interacting with elements.
Step 4: Navigate to the Target Page
driver.get("https://cosmocode.io/automation-practice");
It will make the browser navigate to the URL passed to the get() method.
Step 5: Get Page Title
String pageTitle = driver.getTitle();
System.out.println("Page Title: " + pageTitle);
Step 6: Find and Fill the First Name Field
WebElement firstNameField = driver.findElement(By.id("firstname"));
firstNameField.sendKeys("John");
What's findElement doing here?
The findElement() method searches the page for an element that matches the given criteria. Here, we're looking for an element with id="firstname". Once found, we use sendKeys() to type text into it.
Step 7: Fill the Last Name Field
WebElement lastNameField = driver.findElement(By.className("lastname"));
lastNameField.sendKeys("Doe");
Step 8: Click the Submit Button
driver.findElement(By.id("submit_htmlform")).click();
Step 9: Close the Browser
driver.quit();
What's the difference between close() and quit()?
close()β Closes only the current windowquit()β Closes all windows and ends the WebDriver session
Always use quit() at the end of your test to properly clean up resources.
Complete Code
Put the complete code in one place, man. I don't have time to do copy-paste here and there.
Here you go β
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyFirstSeleniumScript {
public static void main(String[] args) {
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Maximize window
driver.manage().window().maximize();
// Navigate to practice page
driver.get("https://cosmocode.io/automation-practice");
// Print page title
String pageTitle = driver.getTitle();
System.out.println("Page Title: " + pageTitle);
// Fill First Name
WebElement firstNameField = driver.findElement(By.id("firstname"));
firstNameField.sendKeys("John");
// Fill Last Name
WebElement lastNameField = driver.findElement(By.className("lastname"));
lastNameField.sendKeys("Doe");
// Click Submit button
driver.findElement(By.id("submit_htmlform")).click();
// Close browser
driver.quit();
}
}
Running the Script
Wait⦠I am not able to figure out how to run this script? I am completely new to Java.
There are several ways to do that:
In IntelliJ IDEA:
- Right-click on your Java class
- Select "Run 'MyFirstSeleniumScript.main()'"
In Eclipse:
- Right-click on the Class file in Project Explorer
- Select Run As β Java Application
You should see Chrome browser open automatically, navigate to the practice page, fill in the form fields, and then close. Congratulations, you've just run your first Selenium script! π
What Just Happened?
Let's recap what our script did:
- β Launched Chrome browser (driver automatically managed by Selenium)
- β Maximized the browser window
- β Navigated to practice page
- β Located form elements using different locators (ID, Class Name)
- β Typed text into input fields
- β Clicked a button
- β Closed the browser properly
What's Next?
Now that you've got the basics, the real fun begins! In the upcoming tutorials, we'll dive deeper into:
- Different ways to locate elements (XPath, CSS Selectors)
- Handling various elements (dropdowns, checkboxes, radio buttons)
- Dealing with waits and synchronization
- And much more!