Vinod Guneja
Vinod Guneja

Reputation: 15

Define the xpath of the below html for selenium automation

I have HTML which looks basically like the following

  .........

<span class="cat-name">Tanzanite Pendant</span>

  ..........

There are six more span with the same class name.I want to locate only the above span.

I have tried many solution but failed to locate the element.

Thanks, Vinod

Upvotes: 1

Views: 73

Answers (2)

Sumitbit2005
Sumitbit2005

Reputation: 333

in above expression

xpath = //span[@class='cat-name'][n]

xpath = //span[@class='cat-name'][6]

[] has a higher priority than //.

The above expression selects every input element with @class='cat-name', which is the 6th child of its parent -- and apparently the parents of input elements in the document that is not shown don't have so many input children.

Use (note the brackets):

xpath = (//span[@class='cat-name'])[6]

This selects the 6th element input in the document that satisfies the conditions in the predicate.

So you can use below for all six location: In your question use only first one and it will work :

  xpath = (//span[@class='cat-name'])[1]
    xpath = (//span[@class='cat-name'])[2]
    xpath = (//span[@class='cat-name'])[3]
    xpath = (//span[@class='cat-name'])[4]
    xpath = (//span[@class='cat-name'])[5]
    xpath = (//span[@class='cat-name'])[6]

Upvotes: 0

Amith
Amith

Reputation: 7008

From what you have provided,

xpath = //span[@class='cat-name'][n]

Where n can be given values from 1 to 6, which locates the corresponding element.

or

xpath = //span[contains(.,'Tanzanite Pendant')]

Upvotes: 2

Related Questions