Regarding Inject JavaScript Activity, how to use GetElementByClassName or Tagname

Description

Hi Team
i am trying to use Inject JavaScript Activity for my search button click using GetElementByClassName or Tagname since id is not there. Please suggest me for the right one. getting undefined in the alert
Type = Submit, title = Search

Error : Inject Js Script ‘ClickSearch’: TypeError: Cannot read properties of undefined (reading ‘click’)

function clickSearchButton() {
let btn = document.getElementsByClassName(“search-button btn btn-primary ng-binding”)[0];
alert(btn);
if (btn) {
btn.click();
} else {
console.error(“Element not found”);
}
}

Link

http://nourl.com

Date

2024-11-22

Related UiPath products

Studio

@Murali_Boni

Looks like the issue is with line alert(btn) not sure why you gave it

Also if you want to click use class name…in ui explorer you can indicate and select only class as well…even like that the click should work

Cheers

Hi @Anil_G
while inspecting, attached is the DOM for search button, but we are not able to click using JavaScript. Alert i have used just debugging only. in the alert it is coming as 'UnDefined. Please give your thoughts and suggestions.


@Murali_Boni

You get undefined when the element is not identified

So the class name you gave is not getting any element

Ideally if you use click avticuty with type button and innertext search it should work

Cheers

You can’t use multiple classes with getElementsByClassName(). Test to use only one class, e.g. document.getElementsByClassName('search-button')

If it works you can use the code below to filter further:

function clickSearchButton() {
    let elements = Array.from(document.getElementsByClassName('search-button'));
	let filteredElements = elements.filter(el => 
		el.classList.contains('btn') &&
		el.classList.contains('btn-primary') &&
		el.classList.contains('ng-binding')
	);
	
	if (filteredElements.length > 0) {
		let btn = filteredElements[0];
		btn.click();		
	} else {
		console.error('Element Not Found');
	}
}

Hi @ptrobot
Thanks for your reply, but i am getting ‘Element Not Found’. Is there any possibility to finetune your logic plse ?