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

Capybara default_max_wait_time不生效问题求助(Ruby+Kimurai)

Fixing Capybara Wait Time Issues in Kimurai for File Uploads

Let’s work through this step by step to ditch that hacky sleep and get Capybara’s waiting behavior working as expected:

1. Fix the Capybara Configuration Placement

The core issue here is where you’re setting default_max_wait_time. When you configure Capybara inside the parse method, you’re modifying the global config—but Kimurai’s browser instance was already initialized before this runs, so it never picks up the new wait time.

Instead, set Capybara options directly in your Kimurai crawler’s @config hash, so it’s applied when the browser session starts:

require 'kimurai'
require 'pry'

$app_path = 'path_to_the_file'

class Uploader < Kimurai::Base
  @name = "new_google_play_console_app"
  @engine = :selenium_chrome
  @start_urls = ["link_to_the_web_page"]
  # Add this config block to set Capybara options upfront
  @config = {
    capybara_options: {
      default_max_wait_time: 200
    }
  }

  def parse(response, url:, data: {})
    # Verify the setting took effect (should output 200)
    puts browser.default_max_wait_time

    # Rest of your code...
  end
end

2. Optimize File Upload Handling

Your attach_file usage needs a tweak to ensure Capybara properly handles the file upload and waits for completion:

  • Use make_visible: true if your "Browse files" button hides the actual file input (super common with custom UI elements).
  • Move the upload completion check outside the attach_file block—inside the block, you’re only triggering the file selection dialog, not waiting for the upload to finish.

Here’s the revised upload code:

# Click the opt-out button (Capybara will wait up to 200s by default now)
browser.click_button('I’m sure, opt out')

# Handle file upload with proper visibility for hidden inputs
browser.attach_file($app_path, make_visible: true) do
  browser.click_button('Browse files')
end

# Wait for upload completion: adjust the selector/text to match your page's success indicator
# Use `find` to wait for the text to appear (throws an error if it doesn't show up in 200s)
browser.find('*', text: 'App download size', wait: 200)

# Alternative: Wait for an upload progress element to disappear (if applicable)
# browser.has_no_css?('.upload-progress-bar', wait: 200)

3. Ensure the Save Button is Ready to Click

The Unable to find link or button "Save" error happens because Capybara tries to click the button before it’s visible or enabled. Even with the global wait time, explicitly adding wait: to click_on reinforces this behavior:

# Wait up to 200s for the Save button to be present and clickable, then click it
browser.click_on('Save', wait: 200)

Bonus: Check for Iframes

If the upload area or Save button is inside an iframe (common in Google Play Console), you’ll need to switch to the iframe first before interacting with elements:

# Switch to the iframe (replace with your iframe's ID or selector)
browser.switch_to.frame('upload-iframe')

# Do your upload and button clicks here...

# Switch back to the main page when done
browser.switch_to.default_content

Why Your Original assert_text Didn’t Work

  • assert_text throws an immediate error if the text isn’t found, whereas find waits for the element to appear (respecting your set wait time).
  • If the text was inside a nested element or iframe, your original call didn’t target the right context.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 21:38:14