I am trying to programmatically submit text into a text field using watir. I'm using Stack Overflow as a test site for the script.
I was able to reference the search field (name='q'
) but the submit button doesn't have an id
or name
attribute. So how do you reference the submit button programmatically and click it?
This is the code I have so far:
require 'watir'
require 'pry'
browser = Watir::Browser.new :chrome, headless: true
browser.goto 'https://www.stackoverflow.com'
browser.text_field(:name, "q").set('user:963076') #the search bar name='q'
browser.button(#how do I reference the submit button?, '').click
browser.screenshot.save 'screenafter.png' #the screenshot is so that I know what happened after clicking
browser.close
browser.quit
A direct answer to the question is good, but an explanation about how to find solutions to problems like this on your own is better and very much appreciated.
You have to click this button
<button type="submit" aria-label="Search..." class="s-btn s-btn__primary btn-topbar-primary js-search-submit"><svg aria-hidden="true" class="svg-icon mx0 iconSearch" width="18" height="18" viewBox="0 0 18 18"><path d="M12.86 11.32L18 16.5 16.5 18l-5.18-5.14v-.35a7 7 0 1 1 1.19-1.19h.35zM7 12A5 5 0 1 0 7 2a5 5 0 0 0 0 10z"></path></svg></button>
The button has the type attribute with the value of submit
so we can use that to locate that button, here is the code
browser.goto 'https://www.stackoverflow.com'
browser.text_field(name: "q").set('user:963076')
browser.button(type: 'submit').click