We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Write and run your first automated browser script with Selenium.
By the end of this lab, you will be able to:
main method for Selenium.https://www.google.com).driver.quit().This is your first real Selenium script.
Once you understand how to:
…you unlock the ability to automate any web scenario step by step.
If this basic flow is not working, more complex tests will also fail. That's why this "Hello World" of Selenium is critical.
Before starting, make sure you have:
JAVA_HOME set).selenium-basics) with:pom.xml.pom.xml) so everything works smoothly.driver.get(url)driver.quit()driver.quit() to release resources and avoid leaving "orphan" browser processes running.selenium-basicssrc/main/javajava → New → Package.com.dotlog.studentcom.company.project (here: com.dotlog.student).com.dotlog.student package → New → Java Class.FirstSeleniumGoogleIntelliJ will create a file:
src/main/java/com/dotlog/student/FirstSeleniumGoogle.javawith an empty class inside.
FirstSeleniumGoogle in both places). Otherwise, you'll get compilation errors.Replace the entire content of FirstSeleniumGoogle.java with this code:
package com.dotlog.student;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstSeleniumGoogle {
public static void main(String[] args) {
// 1) Create a new Chrome browser window
WebDriver driver = new ChromeDriver();
// 2) Maximize the browser window
driver.manage().window().maximize();
// 3) Navigate to Google
driver.get("https://www.google.com");
// 4) Close the browser and end the session
driver.quit();
}
}new ChromeDriver() will automatically manage the ChromeDriver binary for you, so you usually don't need to download it manually.FirstSeleniumGoogle.java, locate the main method.main method.If Chrome doesn't open or you see errors, check these common issues:
pom.xml under <dependencies>.package com.dotlog.student;pom.xml might be missing the Selenium dependency or it may not have been downloaded.driver.quit() is called.Thread.sleep(3000); before driver.quit() (remember to handle InterruptedException).Thread.sleep() in real test code as a permanent solution. It slows your tests and is unreliable. Later, you will learn to use explicit waits instead.Here is the complete runnable example again:
package com.dotlog.student;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstSeleniumGoogle {
public static void main(String[] args) {
// 1) Create a new Chrome browser window
WebDriver driver = new ChromeDriver();
// 2) Maximize the browser window
driver.manage().window().maximize();
// 3) Navigate to Google
driver.get("https://www.google.com");
// 4) Close the browser and end the session
driver.quit();
}
}WebDriver driver = new ChromeDriver();driver.manage().window().maximize();driver.get("https://www.google.com");driver.quit();driver.quit() in a finally block in real projects to guarantee the browser closes even if a test fails with an exception.GoogleNavigationTest or HomePageSmokeTest so you can quickly see what each test does.main. You'll use test frameworks like TestNG or JUnit and Page Object Model (POM) to structure tests professionally.driver.close() instead of driver.quit()close() only closes one window. quit() shuts down the entire session properly.FirstSeleniumGoogle.java must contain public class FirstSeleniumGoogle exactly.pom.xmlorg.openqa.selenium.WebDriver will be red and the project won't compile.