Code for selenium
Installing Selenium from scratch involves setting up the Java environment, installing the Eclipse IDE, and finally configuring the Selenium libraries.
As of 2026, Selenium 4 is the industry standard. Below is the step-by-step guide to get you up and running.
Phase 1: Install Java (JDK)
Selenium is a Java-based library, so you must have the Java Development Kit (JDK) installed.
Download: Go to the Oracle JDK Downloads or Adoptium (OpenJDK). Download the latest LTS version (e.g., JDK 21 or 25).
Install: Run the installer and follow the prompts.
Set Environment Variables (Windows):
Search for "Edit the system environment variables" in your Start menu.
Click Environment Variables.
Under System Variables, click New and add JAVA_HOME with the path to your JDK folder (e.g., C:\Program Files\Java\jdk-21).
Find the Path variable, click Edit, and add %JAVA_HOME%\bin.
Verify: Open Command Prompt and type java -version. You should see the version details.
Phase 2: Install Eclipse IDE
Download: Visit the Eclipse Downloads page.
Install: Run the Eclipse Installer. Select "Eclipse IDE for Java Developers".
Launch: Open Eclipse and select a folder for your Workspace (this is where your code will be stored).
Phase 3: Download Selenium Libraries
Unlike standard software, Selenium "installation" for Java means downloading .jar files and adding them to your project.
Download JARs: Go to the Selenium Downloads page.
Select Java: Look for the Java section and click the download link (it will be a ZIP file).
Extract: Unzip the folder to a permanent location on your computer (e.g., C:\Selenium\).
Phase 4: Configure Selenium in Eclipse
Now, you need to tell Eclipse where the Selenium files are.
Create Project: In Eclipse, go to File > New > Java Project. Name it SeleniumAutomation.
Create Package/Class: Right-click the src folder > New > Package. Then right-click the package > New > Class (name it FirstTest).
Add JARs to Build Path:
Right-click your project name > Properties.
Select Java Build Path on the left.
Click the Libraries tab.
Select Classpath (if using newer Eclipse versions) and click Add External JARs....
Navigate to your extracted Selenium folder. Add the main selenium-java-x.x.x.jar file and all JAR files inside the lib folder.
Click Apply and Close.
Phase 5: Browser Drivers (Selenium Manager)
In Selenium 4, you no longer manually need to download chromedriver.exe or geckodriver.exe for most basic setups. Selenium Manager (included in the JARs) will automatically handle the driver download for you when you run your code.
Sample Test Code
Copy this into your FirstTest.java file to verify the setup:
Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest {
public static void main(String[] args) {
// Selenium Manager automatically finds the driver
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
System.out.println("Title is: " + driver.getTitle());
driver.quit();
}
}
Next Step: Would you like me to show you how to convert this project into a Maven project to manage these dependencies automatically instead of manually adding JARs?
Comments