We use WebDriver setup to control the Chrome browser automatically for testing websites. It helps us check if web pages work as expected without clicking manually.
0
0
WebDriver setup (ChromeDriver) in Selenium Java
Introduction
When you want to test a website's buttons and links automatically.
When you need to check if a web page loads correctly in Chrome.
When you want to run repeated tests on a website without doing it by hand.
When you want to simulate user actions like typing or clicking in Chrome.
When you want to catch bugs early by testing web pages automatically.
Syntax
Selenium Java
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver();
Replace path/to/chromedriver with the actual location of your ChromeDriver file.
This code starts a new Chrome browser window controlled by your test script.
Examples
Sets the ChromeDriver path on Windows and opens Chrome browser.
Selenium Java
System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe"); WebDriver driver = new ChromeDriver();
Sets the ChromeDriver path on Mac/Linux and opens Chrome browser.
Selenium Java
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); WebDriver driver = new ChromeDriver();
Sample Program
This program opens Chrome, goes to example.com, prints the page title, then closes the browser.
Selenium Java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeDriverSetup { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }
OutputSuccess
Important Notes
Make sure the ChromeDriver version matches your installed Chrome browser version.
Close the browser with driver.quit() to free system resources.
Use absolute paths for the ChromeDriver location to avoid errors.
Summary
WebDriver setup lets your test code control Chrome browser automatically.
Set the ChromeDriver path before creating a ChromeDriver object.
Always close the browser after tests to keep your computer clean.