This guide walks you through setting up Selenium WebDriver with Java and Maven inside IntelliJ IDEA, so you can write and run browser automation tests. You’ll create a Maven project, add the Selenium dependency, write a simple test that opens a browser, and run it from the IDE. No prior Maven or Selenium experience is needed, but basic Java and IntelliJ familiarity helps.
What You’ll Need
- IntelliJ IDEA (Community or Ultimate edition, version 2021.1 or newer)
- JDK 8 or later (JDK 11 or 17 recommended)
- Maven (bundled with IntelliJ, no separate install needed)
- A browser like Chrome or Firefox (Selenium will drive it)
- Internet connection to download dependencies
Step-by-Step
- Install IntelliJ IDEA – Download from JetBrains website and install. The Community edition is free and sufficient for this guide.
- Install JDK – If you don’t have one, download from Oracle or use a package manager. IntelliJ can also download a JDK for you.
- Create a new Maven project – Open IntelliJ, click New Project. In the left panel choose Maven. Set the Project SDK (choose your JDK). Name the project (e.g.,
selenium-demo) and set a GroupId (e.g.,com.example) and ArtifactId. Click Finish. - Wait for Maven to initialize – IntelliJ will create a
pom.xmlfile. It may take a moment to download Maven wrapper files. - Add Selenium dependency – Open
pom.xml. Inside the<dependencies>section (if it doesn’t exist, create it), add the following:<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.21.0</version> </dependency>The version number may be different; check the latest stable on Maven Central. Save the file.
- Let Maven download dependencies – IntelliJ will automatically resolve dependencies. You’ll see a progress bar in the bottom right. If not, click the Load Maven Changes icon (circular arrows) in the Maven tool window.
- Create a Java class – In the
src/main/javaorsrc/test/javadirectory, right-click and select New > Java Class. Name itFirstSeleniumTest. - Write a simple test – Replace the class with:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FirstSeleniumTest { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); driver.get("https://www.google.com"); System.out.println("Title: " + driver.getTitle()); driver.quit(); } }This opens Chrome, navigates to Google, prints the page title, and closes the browser.
- Run the test – Click the green arrow next to the
mainmethod and select Run. If Chrome is not installed, you’ll get an error – install it or change toFirefoxDriver(requires GeckoDriver). - Handle driver binaries – Selenium 4 includes Selenium Manager, which automatically downloads the correct driver for Chrome, Firefox, Edge, or Safari. If you’re using an older Selenium version, you’ll need to download ChromeDriver manually and set the system property.
- Create a proper test with JUnit (optional) – Add JUnit dependency to
pom.xml:<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.2</version> <scope>test</scope> </dependency>Then create a test class in
src/test/javawith@Testannotations. - Use Maven to run tests – Open the Maven tool window (right side), expand Lifecycle, and double-click
test. This runs all tests insrc/test.
Common Mistakes
- Wrong Selenium version – Using an old version that doesn’t work with your browser. Always use the latest stable.
- Missing driver executable – With Selenium 3 and earlier, you must download ChromeDriver and set
System.setProperty("webdriver.chrome.driver", "path"). Selenium 4 handles this automatically. - Browser not installed – Selenium cannot launch a browser that isn’t installed. Ensure Chrome or Firefox is present.
- Wrong Maven coordinates – Typos in groupId or artifactId lead to dependency resolution errors.
- Not refreshing Maven – After editing
pom.xml, you must load changes or the dependencies won’t be available. - Using JUnit 4 with JUnit 5 imports – Mixing versions causes confusion. Stick to one.
Tips
- Use
WebDriverManager(from io.github.bonigarcia) to automatically manage driver binaries, even with Selenium 3. It’s a popular library that simplifies setup. - Always call
driver.quit()in afinallyblock or use try-with-resources to avoid browser leaks. - Add a
.gitignorefile to excludetarget/and IDE-specific files. - Use
@BeforeEachand@AfterEachin JUnit to set up and tear down the driver for each test. - Keep your browser updated – Selenium Manager works best with recent versions.
- If you’re behind a corporate proxy, configure Maven settings.xml to use it.
Troubleshooting
- Dependencies not downloading – Check internet connection. In IntelliJ, go to File > Settings > Build, Execution, Deployment > Build Tools > Maven and ensure Always update snapshots is checked. Also try Maven > Reload Project.
- Error:
Cannot resolve symbol 'WebDriver'– Right-click project, select Maven > Reimport. If that fails, check that the dependency is inpom.xmland the version is correct. - Browser launches but test fails – The page may have loaded slowly. Add an implicit wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS). - ChromeDriver version mismatch – Selenium Manager should handle it, but if you see an error about version mismatch, download the correct ChromeDriver manually and set the system property.
- Maven build fails with
No tests to run– Make sure your test class is insrc/test/javaand its name ends withTest(e.g.,MyTest.java). - IntelliJ doesn’t show Maven tool window – Go to View > Tool Windows > Maven.
Frequently Asked Questions
Do I need to install Maven separately?
No. IntelliJ IDEA bundles Maven and uses it automatically when you create a Maven project.
Why do I get a SessionNotCreatedException?
This usually means the browser version is incompatible with the driver. Update your browser and ensure Selenium Manager downloads the matching driver.
Can I use Firefox instead of Chrome?
Yes. Change ChromeDriver to FirefoxDriver and ensure Firefox is installed. Selenium Manager will handle the GeckoDriver.
How do I write a test that actually asserts something?
Use JUnit assertions, e.g., assertEquals("Google", driver.getTitle()). Add JUnit dependency as shown in step 11.
What is the difference between quit() and close()?
close() closes the current browser window; quit() closes all windows and ends the driver session. Use quit() to free resources.
Can I run Selenium tests in headless mode?
Yes. For Chrome, use ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); and pass it to ChromeDriver(options).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t