Locating Elements (Watir) 

Watir (Web Application Testing in Ruby) is an open-source Ruby library for automating web browsers. It interacts with different browsers (Firefox, Chrome, Safari…) the same way people do: clicking buttons, links, populating forms and validating text. But what about locating Elements in Watir?

Watir allows users to locate elements in many ways which represents one of its most important features. With Watir, the user can locate any element by its text, not just links.

No more XPath! 

Directly using powerful :css & :xpath selectors are supported in Watir, but Watir’s goal is to minimize the need to rely on these selectors, because for example, XPath can be difficult to read.

Standard Watir Locators

ID & Index

browser.div(id: ‘user_id’)

#When combined with other locators, index is the last filter to be applied

browser.div(index: 1)

Name & Class Name

browser.text_field(name: ‘new_user_email’)

#This is for locating with a single class only

browser.text_field(class: ‘name’)

Tag Name

browser.element(tag_name: ‘div’)

#You can use browser.element but it is highly recommended to leverage this locator by using the element’s associated method:


Text & Visible Text

#Text evaluates what is in the DOM and Visible Text evaluates what the user can see on the page

browser.button(text: ‘Button’)
browser.button(visible_text: ‘Start’)

“Locating Elements in Watir” Tech Bite was brought to you by Azra Siručić, Junior Test Engineer at Atlantbh.

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

Software DevelopmentTech Bites
February 23, 2024

Background Jobs in Elixir – Oban

When and why do we need background jobs? Nowadays, background job processing is indispensable in the world of web development. The need for background jobs stems from the fact that synchronous execution of time-consuming and resource-intensive tasks would heavily impact an application's  performance and user experience.  Even though Elixir is…
QA/Test AutomationTech Bites
December 22, 2023

Selenium Grid 4 with Docker

Introduction When talking about automation testing, one of the first things that comes to mind is Selenium. Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of…

Want to discuss this in relation to your project? Get in touch:

Leave a Reply