1
<span class="" data-aura-rendered-by="2565:0">Active</span>
<!--render facet: 2567:0-->
<!--render facet: 2568:0-->
</label> == 0ドル
<input type="checkbox" checked="checked" defaultchecked="true" id="2202:0" data-aura-rendered-by="2206:0" data-interactive-lib-uid="11">
</div>

Can anybody help me to identify an xpath for a checkbox. The HTML is dynamic and the page will change.

I have identified the xpath which is as follows:

//span[.='Active']/../../input

But with this xpath when the position of the active checkbox is changed then the script is not able to identify it.

How can I handle this?

Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
asked Jul 17, 2018 at 11:14
4
  • 2
    Possible duplicate of How to handle dynamic changing ID's In XPath? Commented Jul 17, 2018 at 11:43
  • @AlexeyR. In my opinion, the two are similar, but not really duplicates because of the extra complexity in targeting the child form field. Commented Jul 17, 2018 at 13:06
  • @bharat mane Actually i have already done this but sometimes this also breaks. Commented Jul 18, 2018 at 14:33
  • @bharat mane please suggest if you have more to describe on this Commented Jul 18, 2018 at 14:35

2 Answers 2

2

I know this new tags were found in - Lightning Salesforce Domain. The only one option to automate the Lightning Salesforce site is to Locate elements by taking reference of the Field Label name, Placeholders and design XPath using - XPath Axes (Following, following-sibling, Parent, Child, Ancestors).

First: Add more HTML code where i can find out the field name.

Second: Coming to your solution- Check for the element checkbox ID (id="2202:0") which part is continuously changing and which remains same. IF starting some part of the ID would be the same, then Create XPath using starts-with function.

Ex: //input[starts-with(@id,'220')]

Most Important and Suitable format to Locate elements in Salesforce Lightning is using XPath Axes.

In your case XPath is:

//span[contains(text(), 'Active')]//parent::span/parent::label/following-sibling::input[1]
answered Jul 19, 2018 at 9:24
3
  • Let me know if it works for you. Commented Jul 19, 2018 at 11:43
  • What is the concern that in case there is change in the element name like you have mentioned //span[contains(text(), 'Active')]//parent::span/parent::label/following-sibling::input[1] suppose instead of input[1] there is another element name which is replaced with span[1] in that case will webdriver locate this or not. Commented Jul 20, 2018 at 11:49
  • My concern is that if the name of the element is changed then how the element is located. Or there is way that can be used without using element name to locate. i mean we just try to locate the element based on the things which remain always unchanged though there is a change or not on the page Commented Jul 20, 2018 at 11:53
0

I'm assuming that the ID of the element you're seeking is not static and you have no better way to locate the element than to find it by the text "Active" then move up and then down the DOM to locate it.

The key parts of your HTML appear to be:

<div>
 <label>
 <span>Active
 </span>
 </label>
 <input type="checkbox" checked="checked" defaultchecked="true" id="2202:0" data-aura-rendered-by="2206:0" data-interactive-lib-uid="11">
</div>

If there will only ever be one checkbox in the div that contains the Active span, then you are safe to do something like this (C#-ish syntax, pseudocode):

element parent = driver.findElement(By.Xpath(//div[contains(text(), 'Active')]"));
element checkbox = parent.findElement(By.tagName("input"));

This method will work as long as there is exactly one div on the page with the text Active somewhere in the innerText, exactly one input in the div, and the one input is always of type checkbox.

answered Jul 17, 2018 at 11:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.