Suhird Singh
Suhird Singh

Reputation: 133

XPath Syntax for conditions

I am in search of conditional XPath syntax. My xpath is as follows:

//div[@id='contenttext']/form/table/tbody/tr[7]/td[2]/input[1]

Now the scenario is that the above mentioned input field can be found at tr[7] (already mentioned) and tr[6]. So in brief I need one syntax to test that the input field should be present either at tr[7] or tr[6]. i.e.:

//div[@id='contenttext']/form/table/tbody/tr[7]/td[2]/input[1]
//div[@id='contenttext']/form/table/tbody/tr[6]/td[2]/input[1]

Both the above mentioned XPath should be combined and the test should return true if the input[1] is present at either tr[7] or tr[6].

PS: tr[position()=7 or position()=6] is not working. The code tries to find the element at lower position mentioned. i.e tr[6]

Upvotes: 0

Views: 157

Answers (2)

Kenneth Clark
Kenneth Clark

Reputation: 1755

the position method can be provided a range ...

tr[position() >= 6 and not(position() > 7)]

excerpt from What is the xpath to select a range of nodes?

Upvotes: 0

Pratik G
Pratik G

Reputation: 99

You can use pipeline syntax (|) here as below :

//div[@id='contenttext']/form/table/tbody/tr[7]/td[2]/input[1]|//div[@id='contenttext']/form/table/tbody/tr[6]/td[2]/input[1]

Upvotes: 1

Related Questions