TestProject Forum
Powered by leading experts in the test automation community

How to select element whose xpath is changing

I have to select an option from the drop down, but the issue is the drop down xpath changes every time when clicked. Example for the same element xpath is - //[@id=“ui-id-6”] at one time and next time when clicked it can change to //[@id=“ui-id-10”]. I have tried the following word around-

//[contains(@id, 'ui-id)
//
[contains(@class, ‘ui-menu-item-wrapper’)][1]
//*[text()=‘abcd’]

Is there any other way to extract xpath for element that is constantly changing? Thanks

Hello @tasfia.khan and thanks for reaching out.

Currently, we suggest as you have done, creating a generalized XPATH that will locate the element despite the changing ID:

Hi @tasfia.khan
You can try starts-with in your case say //div[starts-with(@id,‘ui-id’)] ,if you see multiple elements then use indexing like following (//div[starts-with(@id,‘ui-id’)])[1] .Also i suggest in order to get unique xpath try giving a workaround like using different attributes of xpath like preceding ,child, parent ,following ,following-sibling ,preceding-sibling .I see those kind of elements frequently in Salesforce and most of the places and i used to find unique xpath using above attributes mentioned.

2 Likes