Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Use Waits in Selenium with Ruby

Use Ruby Selenium explicit waits to poll for the exact browser state your next test action needs, instead of relying on fixed sleeps.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an explicit wait when a Ruby Selenium test needs the browser to reach a particular state: create Selenium::WebDriver::Wait, then call until with a block that returns a truthy value when it is safe to continue. This replaces arbitrary sleep delays with condition-based polling.

Wait for the state your next action needs

An explicit wait repeatedly evaluates a condition until the block returns a truthy value or the timeout expires. For example, check that a submit button is displayed before clicking it:

wait = Selenium::WebDriver::Wait.new(timeout: 10, interval: 0.2)
wait.until { driver.find_element(id: 'submit').displayed? }
driver.find_element(id: 'submit').click

In this example, the element lookup is inside the block, so each polling attempt checks the current DOM rather than retaining an element reference that the page may have replaced. The timeout and interval are illustrative; choose values appropriate to the application and test environment. Selenium’s current guide also shows a two-second timeout and 0.3-second interval as example settings, not universal recommendations. Selenium’s Waiting Strategies guide describes explicit waits as loops that poll for a specific condition before continuing.

Choose timeout, interval, and ignored exceptions

The Ruby wait API accepts a timeout, polling interval, optional message, optional message provider, and exceptions to ignore. The until method returns the truthy value produced by the block, retries configured ignored exceptions, sleeps for the interval between attempts, and raises Selenium::WebDriver::Error::TimeoutError when its deadline passes. Check the API reference matching your installed Selenium gem for version-specific defaults. Ruby Wait API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

By default, Selenium::WebDriver::Error::NoSuchElementError is ignored, which is useful when the target has not yet appeared. You can add another transient exception when there is a reason to retry it:

errors = [Selenium::WebDriver::Error::NoSuchElementError,
          Selenium::WebDriver::Error::ElementNotInteractableError]

wait = Selenium::WebDriver::Wait.new(
  timeout: 10,
  interval: 0.2,
  ignore: errors
)

wait.until { driver.find_element(id: 'submit').displayed? }

Ignore exceptions sparingly. An exception not in the configured list is raised immediately rather than swallowed by the wait loop. A displayed element is also not necessarily ready for every possible interaction: make the condition correspond to what the test will do next.

Explicit and implicit waits are different

Wait type Scope What triggers another attempt? Configuration
Implicit Element-location calls across the WebDriver session A lookup waits for an element to be found Session-level setting; Selenium’s guide says the default is zero
Explicit A particular condition in a wait block The block is evaluated until it returns a truthy value or times out Per wait: timeout, interval, message options, and ignored exceptions

Use an explicit wait when the test depends on a particular state, such as an element becoming visible. Avoid combining implicit and explicit waits: Selenium warns that their interaction can produce unpredictable total wait times. Its guide gives an example where a nominal 10-second implicit wait and 15-second explicit wait can lead to a timeout after 20 seconds. See Selenium’s warning on mixing waits.

Replace fixed sleeps with targeted conditions

A fixed sleep waits the same length of time whether the page is ready quickly or slowly. An explicit wait continues as soon as its condition is true, while still allowing a maximum deadline. Before writing the wait, identify the evidence the next step requires: visibility before typing, for example, or another condition your application exposes. A locator finding an element alone does not establish that it is visible or interactable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot wait failures

  • The wait times out: The block did not return a truthy value before the deadline. Verify that the locator identifies the intended element, that the condition matches the state the page actually reaches, and that the timeout suits the environment.
  • The failure is immediate: The wait only retries configured ignored exceptions. By default, that is NoSuchElementError; other exceptions are not automatically suppressed. Add an exception only when retrying it is appropriate.
  • Wait duration is longer or less predictable than expected: Look for an implicit wait configured elsewhere in the test setup. Selenium cautions against combining implicit and explicit waits.
  • The element is found but the action fails: Finding an element is not proof it is displayed or interactable. Wait for the state needed by the next operation; Selenium’s Ruby example checks displayed? before typing.

Or skip the browser setup

If your goal is a website screenshot rather than an interactive Selenium test, ScreenshotNeo can capture a URL with one GET request. Cookie banners are accepted and removed before the shot, along with known consent banners, newsletter popups, and chat widgets; bot checks, blank pages, timeouts, failed loads, and cache hits are not billed. Its MCP server gives AI agents screenshot, page-info, and PDF-capture tools.

ScreenshotNeo API documentation

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Free includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Learn about ScreenshotNeo, or sign up free.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

What does `wait.until` return?

It returns the truthy value returned by the condition block.

Which error does a Ruby Selenium wait ignore by default?

`Selenium::WebDriver::Error::NoSuchElementError`.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.