Search code examples
ruby-on-railsseleniumweb-scrapingwatir-webdriver

Rails Watir Webdriver - select option not working


I'm writing a web scraper in Rails and can't get Watir to select an option from a select tag. I've tried multiple variations but by all accounts this should work:

browser.select_list(:id, "system").select("PC")

I've tested to make sure both the select box and value are present, I've used sleep to delay the click to allow everything to load, I've used .when_present, I've tried it in pry (I can set and unset checkboxes in pry and see the effect live, not the case with the select box) and I'm out of ideas. There's no error messages, it just doesn't do anything.

Here's the full code:

require 'watir-webdriver'
require 'pry'

browser = Watir::Browser.new
browser.goto 'http://www.co-optimus.com/games.php'

browser.div(id: "system-modal").link(class: "close-reveal-modal").click

sleep 5
browser.select_list(:id, "system").when_present.select("PC")

sleep 1
browser.checkbox(id: "couch").when_present.set

sleep 2
browser.link(text: "Find Games").when_present.click

Solution

  • Firstly, watir-webdriver has been deprecated by watir. If you use use gem "watir", "~> 6.0.0" you do not need to include #when_present (or the sleeps).

    This works for me (it uses Chrome by default now)

    require 'watir'
    
    browser = Watir::Browser.new
    browser.goto 'http://www.co-optimus.com/games.php'
    browser.div(id: "system-modal").a.click
    browser.select_list(id: "system").select("PC")
    browser.checkbox(id: "couch").set
    browser.button(text: "Find Games").click
    

    Using browser = Watir::Browser.new :firefox on my screen is actually giving issues clicking "Find Games" (I'd need to do a workaround with watir-scroll to get past the javascript overlaying), but works with the select list just fine. (This is also with Firefox 50).

    There were some issues with Select Lists using earlier versions of Firefox with geckodriver, so you might need to use Chrome or update your Firefox version.