Chrome初始化时遇Runtime.executionContextCreated错误的Selenium求助
Hey there, that error usually pops up due to a few common issues with Chrome and ChromeDriver compatibility or setup. Let's walk through the most likely fixes step by step:
1. Ensure Chrome Browser & ChromeDriver Versions Match
This is the #1 cause of this error. ChromeDriver is tightly tied to Chrome's version—if they're out of sync, you'll run into context-related errors.
- Check your Chrome version: Open Chrome → go to Settings > About Chrome to see the exact version (e.g., 125.0.6422.112).
- Download the matching ChromeDriver version: Grab the driver that corresponds to your Chrome's major version (e.g., if Chrome is 125.x, get ChromeDriver 125.x).
- Replace your existing chromedriver file with the new one, and double-check the path in your
setPropertyline is correct.
2. Verify Chromedriver File Permissions (Mac/Linux)
On Mac or Linux, if the chromedriver file doesn't have executable permissions, it can cause unexpected errors.
- Open terminal, navigate to the folder where your chromedriver is:
cd /Users/mn/Desktop/project/turv/src/main/ - Grant executable rights:
chmod +x chromedriver
3. Use Selenium 4.6+'s Built-In Driver Management (Easier & More Reliable)
If you're using Selenium 4.6 or later, you don't need to manually set the webdriver.chrome.driver property anymore. Selenium can automatically download and manage the correct ChromeDriver version for you. Here's how to adjust your code:
First, make sure your project has the latest Selenium dependency (for Maven, add this to your pom.xml):
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.21.0</version> </dependency>
Then simplify your code:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Flows { public static void main(String[] args) { // No need for System.setProperty anymore! WebDriver driver = new ChromeDriver(); try { driver.get("https://www.example.com"); // Replace with your target URL System.out.println("Page Title: " + driver.getTitle()); } finally { driver.quit(); // Always quit the driver to clean up resources } } }
4. Kill Stuck Chrome Processes
Sometimes leftover Chrome background processes can interfere with new driver sessions.
- On Mac: Open Activity Monitor, search for "Chrome" and quit all related processes.
- On Windows: Open Task Manager, end all Chrome and chromedriver processes.
Try these steps one by one—version mismatch is almost always the culprit, so start there. Let me know if you still run into issues after trying these!
内容的提问来源于stack exchange,提问作者Mike Nor

