avdhesh maurya
avdhesh maurya

Reputation: 115

Square brackets in Selenium Locators

Just a bit curious to know if '[' has specific meaning while writing locators in selenium. Couldn't find anything on google.

ex. Xpath=//tagname[@attribute='value']

Do they represent some kind of array of elements matching the criteria or does it have some specific meaning. for ex. in json arrays are enclosed in [], something like that.

Upvotes: 0

Views: 838

Answers (2)

Vikas Thange
Vikas Thange

Reputation: 250

In Xpath we can use [ ] for -

  1. Child Index :

Ex. //tr[2] Means second tr child element on page.

  1. condition on attributes :

Ex. //*[@title='Ok'] . -> matches to any tag having attribute "title" with value "Ok"

You can also use [ ] with Xpath axes methods.

Ex. //*[text(),'OK']

Upvotes: 1

Deepak Kumar
Deepak Kumar

Reputation: 106

Do not confuse the use of "[]" in Xpath with its use programming.

"[]" in programming means the index of the element

but

In Xpath "[]" brackets can be used to specify index or conditions for selecting elements . In you an example: XPath ill select element "tagname" which contains attribute having a value equal to "value".

For more details on conditions please refer: https://developer.mozilla.org/en-US/docs/Web/XPath/Snippets

Upvotes: 1

Related Questions