0
<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?

  • 2
    Possible duplicate of How to handle dynamic changing ID's In XPath? – Alexey R. Jul 17 '18 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. – Kate Paulk Jul 17 '18 at 13:06
  • @bharat mane Actually i have already done this but sometimes this also breaks. – vishnu sharma Jul 18 '18 at 14:33
  • @bharat mane please suggest if you have more to describe on this – vishnu sharma Jul 18 '18 at 14:35
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]
  • Let me know if it works for you. – Bharat Mane Jul 19 '18 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. – vishnu sharma Jul 20 '18 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 – vishnu sharma Jul 20 '18 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.