0
votes

I am testing a web-app using Watir-webdriver and Ruby. I need to validate the content of a table on one of the webpages. The table element has no id, so I cannot directly fetch it. It is, however, nested within a div that does contain an id.

Watir-webdriver has a method (.parent) that allows you to grab the parent element of the element you are working with, but what about going the other way?

Is there something along the lines of .child or .child_element that would allow you to grab a child element through the parent element?

I've searched through Watir-webdriver's API and didn't see anything that would be of use. I was wondering if anyone here would know of a method that I missed?

Thank you.

1

1 Answers

1
votes

All of the element locating methods search in the scope of the browser/element they are called in.

For example assume you have the page:

<html>
  <body>
    <table><tr><td>table 1</td></tr></table>
    <div id="div_id">
      <table><tr><td>table 2</td></tr></table>
    </div>
  </body>
</html>

Normally you call the element methods against the browser, which returns the first matching element in the browser:

browser.table.text
#=> "table 1"

By calling an element method on an element (instead of a browser), you are saying to locate the element within the other element. For example:

browser.div(:id => 'div_id').table.text
#=> "table 2"

Will return the first table that is within the div with id 'div_id' (which is in the browser).