Xpath Selectors

From Training Material
Jump to navigation Jump to search

parent, children siblings ancestors, descendents

 / <- root of the page
 /html/body <- root of the page to the html tag to the body tag

walking, 'traversing' a path

 /html/body/div

entering the root of the page, walking into the html tag, down into body, into the first div we encounter

What about the second div?

 /html/body/div[2]

What about grabbing any div we find?

 //div

What about any div, with the id of first_section?

 //div[@id='first_section']

What about the second list item, beneath the div with the id, first_section?

 //div[@id='first_section']//li[2]

What if we do not know what the tag is, but know we want the id of 'second_section'?

 //*[@id='second_section']

What about grabbing the element with the id of 'first_item'?

 //*[@id='first_item']

What about grabbing the element above that? -- what does it grab?

 //*[@id='first_item']/../

And the one above that? -- what does it grab?

 //*[@id='first_item']/../../

And the one above that?

How about grabbing any option item under a select tag?

 //select//option

And the entire document?


Exercises

  1. Grab the input element with the name of 'email'
  2. Grab the element with the value of 'promote'
  3. Do it in a more specific fashion
  4. Grab the element above that
  5. Select all the input elements within that element
  6. Grab just the second input element