How to Install and Run Selenium

How to Install and Run Selenium

Selenium is a popular open-source framework for automating web applications. To get started with Selenium, you need to set up a few tools and write a basic script.


✅ Step-by-Step Guide to Install and Run Selenium

๐Ÿ”ง 1. Prerequisites

Before installing Selenium, make sure you have:


Java JDK installed (for Java users)


Eclipse or IntelliJ (IDE for writing code)


Maven or Gradle (optional but recommended for managing dependencies)


Web browser (e.g., Chrome, Firefox)


Browser driver (e.g., ChromeDriver, GeckoDriver)


๐Ÿ“ฆ 2. Install Java & Set Environment Variable

Download Java: https://www.oracle.com/java/technologies/javase-downloads.html


Set JAVA_HOME in system environment variables


Verify installation:


bash

Copy

Edit

java -version

๐Ÿ‘ฉ‍๐Ÿ’ป 3. Install an IDE

Install Eclipse or IntelliJ IDEA to write and run your Selenium tests.


Eclipse: https://www.eclipse.org/downloads/


IntelliJ IDEA: https://www.jetbrains.com/idea/


⚙️ 4. Create a Maven Project

You can use Maven to manage Selenium dependencies easily.


Sample pom.xml file:


xml

Copy

Edit

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

         http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>

    <artifactId>selenium-demo</artifactId>

    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <!-- Selenium Java -->

        <dependency>

            <groupId>org.seleniumhq.selenium</groupId>

            <artifactId>selenium-java</artifactId>

            <version>4.15.0</version>

        </dependency>

    </dependencies>

</project>

๐ŸŒ 5. Download Browser Driver

ChromeDriver: https://sites.google.com/a/chromium.org/chromedriver/


Place the driver in your project folder or set its path in your system


๐Ÿงช 6. Write Your First Selenium Test

java

Copy

Edit

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class SeleniumTest {

    public static void main(String[] args) {

        // Set path to chromedriver

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


        // Initialize WebDriver

        WebDriver driver = new ChromeDriver();


        // Open a website

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


        // Print the title

        System.out.println("Title: " + driver.getTitle());


        // Close the browser

        driver.quit();

    }

}

▶️ 7. Run the Test

Run the test class from your IDE


Or use Maven:


bash

Copy

Edit

mvn clean test

๐Ÿง  Tips

Always match the ChromeDriver version with your Chrome browser


Use WebDriverManager to avoid managing drivers manually


Add WebDriverManager to pom.xml:


xml

Copy

Edit

<dependency>

    <groupId>io.github.bonigarcia</groupId>

    <artifactId>webdrivermanager</artifactId>

    <version>5.7.0</version>

</dependency>

Usage:


java

Copy

Edit

WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();

Learn Testing Tools Training in Hyderabad

Read More

Bugzilla vs Jira: Which Is More Efficient?

Sauce Labs vs LambdaTest

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Cybersecurity Internship Opportunities in Hyderabad for Freshers

Tosca for API Testing: A Step-by-Step Tutorial