Zglanowany
Zglanowany

Reputation: 23

Xpath not finding element (parent/ancestor)

<div class="slds-show" data-aura-rendered-by="10155:0">
<div class="footer" data-aura-rendered-by="10156:0">
    <div class="slds-grid slds-grid--align-end slds-m-top--large" data-aura-rendered-by="10157:0">
        <div class="slds-show" data-aura-rendered-by="10158:0">
            <button class="slds-button slds-button--neutral slds-m-left--small" data-aura-rendered-by="10159:0">Cancel</button>
            <button class="slds-button slds-button--neutral slds-m-left--small" data-aura-rendered-by="10161:0">Save &amp; New</button>
            <button class="slds-button slds-button--brand slds-m-left--small" data-aura-rendered-by="10163:0">Save</button>
        </div>
    </div>
</div>

This is part of page, on which I will have to click on Save button. Button is not unique and I need to find it throu class attribute from first div (slds-show), or

Can somebody tell me, why this xpath is not finding this element?

//button[parent::div[@class='slds-show'][@class='slds-button slds-button--brand slds-m-left--small']]

I've also try with ancestor, text instead of class and results is the same. Element is not found via Firefox console

Upvotes: 2

Views: 578

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193308

To click on Save button once finding it through class attribute from first div (slds-show) you can use a much simpler and effective xpath as follows :

//div[@class='slds-show']/button[@class='slds-button slds-button--brand slds-m-left--small']

Note : The class attribute slds-button--brand is unique for the Save button.

Upvotes: 1

Marcel
Marcel

Reputation: 1463

An xpath can easily get too complex, you can also try something like this:

  • //button[text()='Cancel']
  • //button[text()='Save & New']
  • //button[text()='Save']

These will return the exact buttons you need. If you're looking for a specific ancestor, include it in your xpath:

//div[@class="slds-show"]//button[text()='Save & New']

Upvotes: 0

Murthi
Murthi

Reputation: 5347

You can try the following xpaths.

  1. //*[@class="slds-show"]/button[text()="Save"]

or

  1. //*[class="slds-show"]/button[@class="slds-button slds-button--brand slds-m-left--small"]

Upvotes: 0

Andersson
Andersson

Reputation: 52685

Try to update your expression as below:

//button[parent::div[@class='slds-show'] and @class='slds-button slds-button--brand slds-m-left--small']

Note that predicate [@class='slds-button slds-button--brand slds-m-left--small'] in your XPath intend to test @class value of parent div, but not target button

Upvotes: 0

Related Questions