Home > Back-end >  Why I can't find element by xpath?
Why I can't find element by xpath?

Time:09-17

I have element:

<span ng-click="openOrderAddPopup()" class="active g-button-style g-text-button g-padding-5px g-margin-right-10px ng-binding"> <i class="fa fa-plus g-margin-right-5px"></i> "Add "</span>

I want to find it by xpath.

This one works: *//span[@class='active g-button-style g-text-button g-padding-5px g-margin-right-10px ng-binding'] but looks ugly and it's not unique because there are three such elements.

I want to find it by text but this both ways doesn't work:

*//span[text()='\"Add \"']

or

*//span[contains(text(),'Add')]

CodePudding user response:

If you see double quotes like this "Add" in HTML DOM. Then it not a plain text that you can use it with text() method of xpath.

It is a text node. which you can defined in xpath v1.0 which Selenium usage.

I would advise you to try this xpath:

//span[@ng-click='openOrderAddPopup()' and contains(@class,'active')] 

if this is not unique and have multiple entry then least preferable choice is xpath indexing.

(//span[@ng-click='openOrderAddPopup()' and contains(@class,'active')])[1]

to select the first element, [2] to select the second element and so on...

  • Related