How to Identify Elements in Salesforce Lightning for Selenium

3.3k views Asked by At

I am trying to automate Salesforce lightning using Selenium, but getting issues with identifying elements. Reason, its having dynamic IDs , and other attributes are either very long , or they are not unique.

For eg ,

<a id="170:1968;a" class="textUnderline outputLookupLink slds-truncate forceOutputLookup" 
data-refid="recordId" 
data-recordid="0059E000001aOCSQA2" 
data-special-link="true" 
href="#/sObject/0059E000001aOCSQA2/view" 
target="_blank" rel="noreferrer" 
title="" data-aura-rendered-by="170:1968;a" data-aura-class="forceOutputLookup"/>

In above code , ID is dynamic , Class is not unique, and all the Lookup elements are associated with it. Also the absolute path is not much trusted , and hence I am trying to find any concrete option to handle these elements. Any help will be highly appreciated.

3

There are 3 answers

1
Gaurav Thantry On

Here, you could try using the contains method if at least a part of the id attribute value is static. From your code, you could try

//a[contains(@id,"a")]/ //--extended xpath--

From the given html code, the 'a' in the id attribute of the a tag looks static, while the rest changes.

0
Deepak On

You can ask the developers to provide an id to the lightning component using aura:id Then the dynamic id won't be generated.

0
万明辉 On

You can try with field labels and fetch its parent node(s), and then fetching childs or brother nodes to locate related texts/text boxes etc.

Eg. You are in Account Edit/New page, and you want to fill in a value to the text box for Account Name field. So you can firstly try with //*[text()='Account Name']/parent::* to find an element that covers BOTH the field label and the text box.

And then you can check if the text box is a 'brother' or a 'child'. If it's a 'child' then try with //*[text()='Account Name']/parent::*(/parent::*)//*[attributes for the text box]; If it's 'brothers' then try with //*[text()='Account Name']/parent::*(/parent::*)/following-sibling::*[attributes for the text box]

You can use this logic to locate all type of fields in all standard lightning pages.