52

On my HTML page I have forty divs but I only want one div.

Using agility pack to search and get all the divs with Ids I use this XPath:

"//div[@id]"

But how do I search for divs with Ids where the id contains the text "test" like so:

<div id="outerdivtest1></div>"
Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Hello-World
  • 9,277
  • 23
  • 88
  • 154

4 Answers4

106

Use the contains function:

//div[contains(@id,'test')]
choroba
  • 231,213
  • 25
  • 204
  • 289
4

I've used this with for the CSS class:

//div[@class = 'atom']

I assume it's similar with id's.

Remy
  • 12,555
  • 14
  • 64
  • 104
2

You can use the xpath

//div[@contains(@id,'test')]

If you want to use the first occurrence, it works fine but if it's not the first occurrence you have to go with different xpath specific to the particular element.

ekad
  • 14,436
  • 26
  • 44
  • 46
-1

For example if you want first selection "id" is like that (//[@id="numberIdentify"])//following:://a[contains(text(),'V ')]