Classic Selector has no fuzzy selection for dynamically generated ID random number. Javascript solution?

i understand that normally we edit the selector and replace the number with a wildcard such as ‘*’. however, even this is unstable…

How do we use Javascript to say, select a pulldown menu element called “Accepted” with a class called “sfDropDownContainer”?

when i use the code below inside an injectJS activity, i get the error message : Inject Js Script ‘SELECT 112:’: TypeError: Cannot read properties of undefined (reading ‘length’)
Invoke PT_Search workflow: TypeError: Cannot read properties of undefined (reading ‘length’)
RemoteException wrapping UiPath.Core.ElementOperationException: TypeError: Cannot read properties of undefined (reading ‘length’) —> RemoteException wrapping System.Runtime.InteropServices.COMException: TypeError: Cannot read properties of undefined (reading ‘length’)
at UiPath.UiNodeClass.InjectAndRunJS(String bstrCode, String bstrInputText)
at UiPath.Core.UiElement.InjectAndRunJS(String code, String input)
— End of inner exception stack trace —
at UiPath.Core.Activities.ObsoleteTaskAsyncCodeActivity`1.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UiPath.Executor.BaseRunner.EndExecute(IAsyncResult result)
at UiPath.Executor.InProcessRunner.EndExecute(IAsyncResult result)
at UiPath.Core.Activities.ExecutorInvokeActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at UiPath.Executor.BaseRunner.EndExecute(IAsyncResult result)
at UiPath.Executor.InProcessRunner.EndExecute(IAsyncResult result)
at UiPath.Core.Activities.ExecutorInvokeActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

JS Code :

function selectAcceptedOption() {
var dropdown = document.querySelector(“.sfDropDownContainer”);
if (dropdown) {
for (var i = 0; i < dropdown.options.length; i++) {
if (dropdown.options[i].innerText === “Accepted”) {
dropdown.selectedIndex = i;
break;
}
}
var event = new Event(“change”);
dropdown.dispatchEvent(event);
} else {
setTimeout(selectAcceptedOption, 100); // Check again in 100 milliseconds
}
}

setTimeout(function(){
selectAcceptedOption();
}, 1000); // wait for 1 second before calling function

@byewiper

If dropdown is the main element elcosing all the options then dropdown.Length is what you need to use. please try

Also you can try acheiving this my creating regex selectors also if needed…

Cheers