You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio Code中使用Java安装Selenium WebDriver?

Installing Selenium WebDriver with Java in Visual Studio Code

Hey there, I’ve got you covered with a step-by-step guide to get Selenium WebDriver up and running with Java in VS Code. Let’s dive right in:

Prerequisites First

Before we start, make sure you have these set up:

  • JDK 8 or higher: Verify installation by running java -version and javac -version in your terminal. If missing, grab the latest from Oracle or OpenJDK.
  • VS Code: Ensure it’s updated to the latest version (you probably already have this, but double-check!).

Step 1: Install Required VS Code Extensions

Open VS Code’s Extensions tab (Ctrl+Shift+X) and install these must-haves:

  • Extension Pack for Java: Bundles all core tools for Java development (language support, debugger, project management, etc.).
  • Test Runner for Java: Makes running and debugging Selenium tests straightforward.

Step 2: Create a New Java Project

  1. Open the Command Palette (Ctrl+Shift+P) and type Java: Create Java Project.
  2. Choose Maven (recommended for easy dependency management) or No build tools for a minimal setup.
  3. Name your project (e.g., SeleniumDemo) and pick a save location.

Step 3: Add Selenium WebDriver Dependencies

If you went with Maven:

  1. Open the pom.xml file in your project root.
  2. Insert these dependencies inside the <dependencies> tag (use the latest versions available):
<!-- Selenium Java Core -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.15.0</version>
</dependency>
<!-- WebDriverManager (auto-manages browser drivers, no manual downloads) -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.6.0</version>
</dependency>
  1. Save the file—VS Code will automatically download and configure the dependencies for you.

Option B: Manual Setup (No Build Tools)

  1. Download the Selenium Java bundle (contains all required JARs) from the official Selenium release page.
  2. Extract the ZIP, create a lib folder in your VS Code project, and move all Selenium JARs into it.
  3. Right-click the lib folder in VS Code and select Add to Java Build Path.
  4. If using WebDriverManager, download its JAR and add it to the build path the same way.

Step 4: Configure Browser Driver (Skip if Using WebDriverManager)

If you’re not using WebDriverManager, you’ll need to download a driver matching your browser version:

  • ChromeDriver for Chrome, GeckoDriver for Firefox, EdgeDriver for Microsoft Edge.
  • Place the executable in a folder (e.g., C:\drivers on Windows or ~/drivers on macOS/Linux).
  • Add this line to your code to point to the driver path:
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");

Step 5: Write a Sample Test

Create a new Java class (e.g., FirstTest.java) in the src folder and add this code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class FirstTest {
    public static void main(String[] args) {
        // Auto-setup ChromeDriver with WebDriverManager
        WebDriverManager.chromedriver().setup();
        
        // Launch Chrome browser
        WebDriver driver = new ChromeDriver();
        
        // Navigate to Google
        driver.get("https://www.google.com");
        
        // Print page title to confirm success
        System.out.println("Page Title: " + driver.getTitle());
        
        // Clean up and close the browser
        driver.quit();
    }
}

If skipping WebDriverManager, replace the WebDriverManager line with the System.setProperty code from Step 4.

Step 6: Run the Test

  • Right-click inside the code editor and select Run Java, or use the Test Runner extension to execute it.
  • You’ll see a Chrome window open, navigate to Google, print the page title in the terminal, then close automatically—this means your setup works!

Troubleshooting Tips

  • Driver not found error: Double-check the driver path (if manual) or ensure WebDriverManager is correctly added to dependencies.
  • Java not recognized: Confirm your JDK is in the system PATH, or configure it via Java: Configure Java Runtime in the Command Palette.
  • Dependency issues: For Maven, verify version numbers in pom.xml and wait for VS Code to finish downloading dependencies.

内容的提问来源于stack exchange,提问作者Aravind TK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:47:34