Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Locating elements

Sean Gregory edited this page Oct 9, 2021 · 1 revision

Locating elements

The browser supports a list of methods that will return an element object methods called on that element object will locate the element

This library supports locating objects by arbitrary attributes, with the value being a String or Regex The regex support is limited to anchors, and the case insensitive flag

Given the following html

<body>
  <div id="this-is-a-div">Div content</div>
</body>
div = browser.div(id: "this-is-a-div")
# or 
div = browser.div(id: /THIS-IS-A-DIV/i)

div.text # Div content

nested elements and collections

Elements can be located from a collection, and nested. This will find the a tag with an id of link inside the second div found in the dom, and click it

browser.divs[1].link(id: "link").click

force locating elements

If the element is stored to a variable, and is already located, it will use that id to interact with it by default

This could cause issues with a StaleElementReferenceException if the id becomes out of scope.

By using the force: true option, the element as well as all of it's ancestors will be relocated.

div = browser.body.div(id: "div")
div.text
browser.goto "http://www.another.page.com"
div.text # raises StaleElementReferenceExcpetion
div.text(force: true) # relocates the div and body elements

Notes

Elements can also be located by using xpath or css directly.

This is highly not recommended, as elements that use these attributes cannot be force relocated, and will have unexpected consequences.

Clone this wiki locally