Ask a Question

How to extract selector from name property of web object

Occasional Contributor

How to extract selector from name property of web object

I have troubles getting the selector from the name property.
Example name property: FindElement("[data-test=\'scene-list-panel-header\'] > [data-test=\'list-panel-header-action\']")


What i try to do is to use the selector for the .FindElement() but before that adding its child to the xpath.
This would looke something like (only for example):


var nameProperty = GetNameProperty(ListItemObject) // -> which will result having the value like e.g.: FindElement("//*[@data-test=\'scene-list-item\' and @data-test-scene-anker=\'TestAnker\' and descendant::*[@data-test=\'scene-list-item-subject\' and normalize-space(text())=\'TestSubject\']]")

//then remove all unnecessary signs/characters
var cleanXpath = aqString.Remove(NameProperty, "\\", "");
cleanXpath = aqString.Remove(cleanXpath, "FindElement(\"", "");
cleanXpath = aqString.Remove(cleanXpath, "\")", "");

// as last step i want to use the .FindElement() method to get the correct button
var mainPage = NameMapping.System.Browser.MainPage; //main page object to use the .FindElement method on

var childXpath = "//*[@data-test='scene-list-item-edit-button']

var correctButton = mainPage.FindElement(cleanXpath+childXpath);


I know i could use the FindChildByXpath method instead, but it seems to be unreliable (sometimes my test finds the child and sometimes it cant find the child)

My problem here is that i cant seem to run the aqString.Remove method on the name property. When i try to remove the back slashes aqString.Remove throws a type conflict error

Is there maybe a better way to get the clean xpath/selector from the name property?

Occasional Contributor

I managed to remove the backlslashes by using JScript's .remove() method instead.

-> testString.replace(/\\/g,"");

SmartBear Alumni (Retired)

Thank you for sharing!

Sonya Mihaljova
Community and Education Specialist

Showing results for 
Search instead for 
Did you mean: