Inject JS to find Display or Visibility of element on webpage

I’m just trying to get inject JS to return a value of the display or visibility of a pop up window on a web app. When I run my Inject JS I get an error HRESULT: 0x80020101. Which is supposed to indicate my syntax is wrong and I feel like it has something to do with the apostrophe or quotation marks that I’m using…?

I didn’t want to attach an external JS file because it says it can be written directly in as a string, and it’s simple enough I think it should just be inline in the activity.

Can someone attach an example of how to do this? Because mine isn’t returning anything and I keep getting the same error. If this activity is contained within an attach browser activity, does it make a call to the webpage as a whole or do I need to include a selector for the specific element I’m trying to find the visibility/display of?

Thanks for your help ::coffee: :slightly_smiling_face:

Could I see an example of your syntax

Currently I am working on such a component and ALPHA is soon ready.
I have the plan to relase it on UiPath Go/ConnectThis text will be hidden It’s done

please paste your javascript here and I will have look on it.
Post also a screenshot of your invoke js config

1 Like

You do not need to include a selector. The inject activity acts pretty funny with its return values. For some reason I could not just return the variable I had to do it like this…

var x = document.GetElementById(‘yourID’)
return (‘’ + x + ‘’);

I can not remember why? Remember if you get an element by type or class it will return an array of elements even if there is only one.

This is what I have now. I’ve changed some things around a bit. But same Idea. I don’t think it’s a syntax error any more. I think I have that working now, but I’m getting a null reference error. There is a pop up window on the page. I inspected the element and the id on the webpage is ‘messageAlert0’ So this is what I have in the ScriptCode input:

       return document.getElementById('messageAlert0').display;

This is the error I’m getting.

UiPath.Core.ElementOperationException: Unable to get property ‘display’ of undefined or null reference ----> System.Runtime.InteropServices.COMException: Unable to get property ‘display’ of undefined or null reference
at UiPath.UiNodeClass.InjectAndRunJS(String bstrCode, String bstrInputText)
at UiPath.Core.UiElement.InjectAndRunJS(String code, String input)
— End of inner ExceptionDetail stack trace —

@HsDev yes the issue is the not available propertx display. Check your messages for my notice

1 Like

Thank you @ppr if you can help me when you are off work that would be great. If anyone can explain in the meantime, I’m at work currently trying to figure this out, so I would appreciate any info! I’ve been stuck on this for awhile.

Give me one hour then I am at home and will connect to you

1 Like

Awesome. I may be on lunch at that time, but will review if you’ve posted when I get back. I appreciate your help. :slightly_smiling_face:

The inject script was struggling with returning a variable so I would guess it would have trouble directly returning what you attempted as well. So I would assign it first

       let x =  document.getElementById('messageAlert0').getAttribute('display');
       return ("+x+");

Normally it would just be “return x” . I am referencing old snippets I have and cannot remember why “return (”+x+“)” is necessary. If the element is not found it could return null or an empty string so you should test for that. I think you are more likely looking for the property “visibility” and not “display” . If those properties do not give you the results you need check to see if the “z-index” property is changing and then possibly the size and position of the popup.

1 Like

@HsDev Which UiPath Version and Edition are you using?

Studio 2018.4.5 - 03/27/2019
Enterprise Edition

It’s not liking the syntax of those double quotes adding the variable to the string. But still testing. :slight_smile:

Sorry. Aside from the outside quotes the others are two single quotes back to back each time. I did not realize before when I was digging up my snippets. I got it though…The activity is extremely picky so idk if it needs everything but copy this exactly. Including spaces, semi-colons…everything

"function (e, b) {
      let x = document.getElementById('' + b +'');
	  let y = x.getAttribute('data-game');
	  return ('' + y + '');

I believe you need to input your search string as a parameter. I just did a simple assign to the “b” value. In the Input parameter field of the inject js activity you only put that ONE parameter. Although your function should have two. The first one is a default error variable or whatever. Lines 2 & 4 are single quotes back to back. If you are still struggling lmk and I’ll send you a xaml to dissect. I recommend saving this snippet because this was just silly.:sleeping:

It says it can take an element as input, how do I do that. Should I just fill in the selector field? Or pass an element in? And then it just uses that as the first parameter of the activity?

   Function(element, input){   ...   }

How do I make use of the selector field or the element field in the properties panel of this injectJS activity?

@HsDev an you check on sample xaml the properties for the inject js Acitivity

Javascript: Function(element, input) here its passed
Element is the uielement - check target and see the use of the variable element
Parameters: the input


after rethinking an additional answer came up into my mind. Instead of the indicate on screen activity you can use a find element activity. Output of this assign to variable uiElement (see screenshot above)

1 Like

Okay, so If I want to directly pass in the UiElement as an argument in my function how do I do that…

So if I do a find element activity, and it returns an element I named box.
Then I put box in as the Target.Element in the properties of the inject js activity?
And then when I write the actual function to do something with that element, do I include the variable name? Does it matter? Does it get passed in anyways?

 Function(box){---} ..... Function(e){---} 

or do I have to do something like this…


I’m also curious how the input gets passed in.

Do you have to include the variable name in your function input parameters?

the returned uielement (variable box in your case) wil be assigned to invoke js target property element (refering to screenshot: here it is similar uiElement)

I would suggest to implement the javascript method signature not with changed name and implementing it with Function(element, input){—}

input configured as described above