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

Python新手求助:Selenium自动化Electron应用Piskel失败问题

Fixing Selenium Remote + Electron Automation Issues (Piskel App)

Hey there, let's work through the issues you're hitting with automating Piskel (and Electron API Demos) via Selenium Remote. Those DevToolsActivePort and AttributeError errors are super common when getting Electron apps hooked up to Remote WebDriver, so let's break down the fixes step by step.

1. Resolve the DevToolsActivePort file doesn't exist Error

This is the root cause of your app timing out and closing immediately. Electron doesn't enable the Chrome DevTools port by default, which ChromeDriver needs to communicate with the app. Here's how to fix it:

  • Configure ChromeOptions correctly: You need to point ChromeDriver directly to your Electron app's binary, explicitly enable a debugging port, and set a temporary user data directory (to avoid conflicts with existing profiles).
  • Pass these options to Selenium Remote: Since you're using a Selenium Server, these capabilities need to be sent to the remote hub so it knows how to launch the Electron app properly.

Example Python Code for Setup

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def get_electron_remote_driver():
    chrome_options = Options()
    
    # Replace with your actual Electron app binary path
    # Linux example: "/home/user/piskel/piskel"
    # Mac example: "/Applications/Piskel.app/Contents/MacOS/Piskel"
    chrome_options.binary_location = "/path/to/your/piskel/binary"
    
    # Critical flags for Electron <-> ChromeDriver communication
    chrome_options.add_argument("--remote-debugging-port=9222")  # Use any unused port
    chrome_options.add_argument("--user-data-dir=/tmp/electron_test_temp_profile")  # Temp dir to avoid conflicts
    
    # Connect to Selenium Remote Server
    driver = webdriver.Remote(
        command_executor="http://localhost:4444/wd/hub",
        desired_capabilities=chrome_options.to_capabilities()
    )
    return driver

# Test the setup
if __name__ == "__main__":
    driver = get_electron_remote_driver()
    # Now you can run your automation steps, e.g.:
    # driver.find_element_by_css_selector("#some-piskel-element").click()
    driver.quit()

2. Fix Your Selenium Server Launch Command

Your original command has a formatting issue (missing space after chromedriver path) and may have version mismatches:

  • Add a space between the chromedriver path and -jar flag
  • Ensure ChromeDriver version matches Electron's Chrome version: Every Electron release bundles a specific Chrome version. Check Piskel's about menu or run electron --version to find the Chrome version, then download the exact matching ChromeDriver.

Corrected Server Command

java -Dwebdriver.chrome.driver="./driver/linux64/chromedriver" -jar selenium-server-standalone-3.141.59.jar

3. Fix the AttributeError: 'Context' object has no attribute 'driver'

This is a test framework (likely Behave, since you mentioned after_scenario hooks) initialization issue. You need to:

  • Explicitly assign the driver to the context object in your before_scenario hook
  • Guard against uninitialized driver in after_scenario to avoid errors if setup failed

Example Behave Hook Fix

# environment.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def before_scenario(context, scenario):
    try:
        chrome_options = Options()
        chrome_options.binary_location = "/path/to/your/piskel/binary"
        chrome_options.add_argument("--remote-debugging-port=9222")
        chrome_options.add_argument("--user-data-dir=/tmp/electron_test_temp_profile")
        
        context.driver = webdriver.Remote(
            command_executor="http://localhost:4444/wd/hub",
            desired_capabilities=chrome_options.to_capabilities()
        )
    except Exception as e:
        print(f"Failed to initialize driver: {e}")
        raise

def after_scenario(context, scenario):
    # Check if driver exists before trying to quit
    if hasattr(context, 'driver'):
        context.driver.quit()

4. Resolve MacOS Mojave's Failed to create a Chrome process Error

On Mac, Electron apps are packaged as .app bundles—you need to point to the actual binary inside the bundle, not the .app file itself:

  • Correct binary path: /Applications/Piskel.app/Contents/MacOS/Piskel
  • Check permissions: Ensure ChromeDriver has execute permissions (chmod +x /path/to/chromedriver) and that your system security settings allow the driver to run (you may need to approve it in System Preferences > Security & Privacy)

Final Notes

  • Port conflicts: If port 9222 is in use, switch to another unused port (e.g., 9223) in both the ChromeOptions and ensure nothing else is using it.
  • Clean temp directories: Delete the /tmp/electron_test_temp_profile folder between tests to avoid leftover state causing issues.
  • Version lock: Always match ChromeDriver to Electron's Chrome version—this is the #1 cause of weird Electron automation errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:36:15