JS Inject - Get JS to click on a specified selector or xpath target?

Is it possible to use JS Injection to run code that click on a passed in selector/xpath target ?

Not looking for a click trigger of JS. Looking to see if a workflow has a specific web page up, attaches to it, and it JS Injects code which simulates a mouse click on a provided target.

Is this possible ?

If so, provide sample simple JS code example ?

2 Likes

Hey @riverrat437

This looks possible with the below JS method

Document.querySelector()

Ref - Document.querySelector() - Web APIs | MDN

Hope this helps

Thanks
#nK

1 Like

I found a way to do it. I’ll share my draft working JS shortly. Way from my computer currently.

1 Like

This is a hard coded example:

function clickOnElement(element, arg1)
{
	// NOTE: UiPath JS Inject gives a SINGLE input argument for using Javascript Inject.
	// As such, if multiple values are required, JS would need to take arg1 and split
	// it on some separation character to get the other values.
	// This was developed from: https://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript
	// Difference is that is a SIMULATED click and this needed a PHYSICAL click ... which .click() by itself does
	// Inputs:
	//	arg1 = Target string to click on

	//var targetString = arg1;
	var mgPlusElementToClick = document.querySelector("#orderlineAdUnitGrid\\.dataGridView > div > div.slickGridHolder.slickgrid_341831.ui-widget > div.slick-viewport > div > div:nth-child(11) > div.slick-cell.l1.r1 > span.toggle-10.makegood.expand.fa.fa-plus-square-o")
	mgPlusElementToClick.click();

	return("Clicked !!!");
}

Note that using the Chrome Developer Tools console will save your sanity. Especially in figuring out what selectors should look like:

… like that double backslash within #orderlineAdUnitGrid\\.dataGridView.

Looking at other forums on finding a list of “targets” using querySelector can likely be used to either loop within injected JS and clicking on them or passing up a CSV of targets and having UiPath loop and click on them.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.