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

Ruby Selenium运行无头Chrome失败问题求助

Troubleshooting Headless Mode Failures with Ruby + Selenium on Windows 10

Hey there! I’ve run into this exact headless mode headache on Windows before—let’s walk through the most likely fixes to get your scripts working smoothly.

First, let’s tackle the biggest culprit that trips up almost everyone: Chrome and ChromeDriver version compatibility. Headless mode is way pickier about matching versions than regular browser mode. Here’s what to do:

  • Open Chrome, go to chrome://settings/help to grab your exact browser version (e.g., 118.0.5993.70).
  • Download the matching ChromeDriver build (search for "ChromeDriver download" and pick the version that aligns with your Chrome’s major/minor release).
  • Make sure ChromeDriver is either in your system PATH, or explicitly specify its path in your Ruby code like this:
    Selenium::WebDriver::Chrome.driver_path = 'C:/Users/YourName/Downloads/chromedriver.exe'
    

Next, Windows has some quirky requirements for headless Chrome that regular mode skips over. Let’s adjust your options to cover these edge cases. Replace your basic headless arguments with this more robust set:

require 'selenium-webdriver'

options = Selenium::WebDriver::Chrome::Options.new
# Use the updated headless mode (required for Chrome 112+)
options.add_argument('--headless=new')
# Set a realistic window size—headless defaults to tiny, which breaks some page layouts
options.add_argument('--window-size=1920,1080')
# Fixes common memory/rendering glitches on Windows
options.add_argument('--disable-dev-shm-usage')
# Bypass sandbox restrictions (safe for testing environments)
options.add_argument('--no-sandbox')
# Disable extensions that might interfere with headless execution
options.add_argument('--disable-extensions')

driver = Selenium::WebDriver.for :chrome, options: options
begin
  driver.get('https://google.com')
  puts "Page loaded successfully! Title: #{driver.title}"
ensure
  driver.quit
end

If that still doesn’t work, let’s rule out other potential issues:

  • Update your Selenium gem: Run gem update selenium-webdriver in your terminal—older versions might not support the new headless flag or Windows-specific bug fixes.
  • Check for system interference: Windows Defender or third-party antivirus tools sometimes block headless Chrome processes temporarily. Try disabling them briefly for testing (or add ChromeDriver/Chrome to your allowed apps list).
  • Enable debug logging: Add this line at the top of your script to see exactly where it’s failing:
    Selenium::WebDriver.logger.level = :debug
    
    The logs will tell you if Chrome failed to start, couldn’t load a page, or ran into a JavaScript error—super helpful for narrowing down the root cause.

One last note: If you’re using an older Chrome version (pre-112), the --headless=new flag won’t work—stick with the original --headless flag instead. But I strongly recommend updating Chrome to the latest stable version to avoid other bugs.

Give these steps a shot, and if you hit a specific error message, feel free to share it—I can help dig deeper!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:14:07