MichaelR
MichaelR

Reputation: 999

Watir-webdriver: Locating the last ul element on page

Lets say ive got several( I dont know how many ) ul elements, Unfortunately they all have the same class, and their ids are generated randomly. All i know is that the one i want is the last one.

Is there a way to easily locate it?

Upvotes: 0

Views: 251

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

You can get a collection of the ul elements (using uls) and then take the last one (using last).

For example, given the page:

<html>
  <ul><li>1</li></ul>
  <ul><li>2</li></ul>
  <ul><li>3</li></ul>
</html>

Then you can get the collection (ie all) ul elements with:

browser.uls

The ul collection includes a last method to get the last element:

browser.uls.last
#=> #<Watir::UList:0x4e3c436a located=false selector={:element=>(webdriver element)}>

browser.uls.last.text
#=> "3"

Upvotes: 1

Related Questions