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

Chrome初始化时遇Runtime.executionContextCreated错误的Selenium求助

Fixing "Runtime.executionContextCreated has invalid 'context'" Error in Selenium ChromeDriver

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 setProperty line 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:48:58