ruby ่‡ชๅ‹•ๅŒ–

Overview and Usage of Selenium Webdriver in Ruby

Shou Arisaka
1 min read
Oct 12, 2025

Webdriver enables browser automation. In addition to UI testing, it's also useful for having browsers automatically perform various operations.

Installation

gem install selenium-webdriver

Reference

[https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings ](https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings) [http://seleniumhq.github.io/selenium/docs/api/rb/_index.html ](http://seleniumhq.github.io/selenium/docs/api/rb/_index.html)

element Class Methods

Selenium::WebDriver::Element. Methods like click and send_key are commonly used.

[http://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Element.html ](http://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Element.html)

Waiting for a Certain Time

It's possible to make it wait until an element appears

Explicit Wait

  wait = Selenium::WebDriver::Wait.new(timeout: 3)
  wait.until { driver.find_element(id: "cheese").displayed? }

Implicit Wait

  driver = Selenium::WebDriver.for :firefox
  driver.manage.timeouts.implicit_wait = 3 # seconds

JavaScript Dialog Handling

You can use WebDriver to handle Javascript alert(), prompt() and confirm() dialogs. The API for all three is the same.

You can handle alerts and such. It's possible to prevent alert automation interference.

Sample

driver.find_element(name: 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end

Debugging

Output full logs Selenium::WebDriver.logger.level = :debug

Write to file Selenium::WebDriver.logger.output = 'selenium.log'

Share this article

Shou Arisaka Oct 12, 2025

๐Ÿ”— Copy Links