cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mus
Regular Visitor

Javascript won't execute on web page

Hi,

 

I'm running quite complicated Desktop flows so far, and more or less successfully, but now I have one challenge I need a support from community. I learned myself how to program in VBA, JavaScrip, but I can't consider myself a full-fledged programmer 🙂

 

I am facing the following problem: I want to execute JavaScript on page with dynamic menu, so I need to find a location of element (Button) each time I entering this page. My idea is to locate the element by it's name.

To illustrate better, below are example of the same element JS Path at different times of the visit:

document.querySelector("#commandBarItemButton_6")
document.querySelector("#commandBarItemButton_304")
document.querySelector("#commandBarItemButton_578")

etc.

 

In the Browser console I can use the following code which all works on any given circumstance:

document.querySelectorAll('[aria-label="BUTTON NAME"]')[0].click();

document.querySelector('[aria-label="BUTTON NAME"]').click();

Even with jQuerry: $('[aria-label="BUTTON NAME"]').click();

 

But as soon as mentioned code is added in PA "Run JavaScript function on web page" it is just not working ... just nothing happens.

This method works on other pages, but not on this particular.

 

What I'm doing wrong here? Or this could be some page restrictions I'm not aware about?

6 REPLIES 6
VJR
Super User
Super User

Hello @Mus 

 

Since you are using Power Automate Desktop, is any specific reason why are you looking to inject javascript to click the button?

You can spy that element and perform any of the UI Automation options that PAD gives.






-------------------------------------------
Help fellow beings find relevant posts.
Upvote to a supporting response.
Mark as solution to an answer.

Mus
Regular Visitor

Hi @VJR,

 

I thought this would be much easier approach instead of using UI. As if I'm picking up the UI element, it picks up so many lines with a lot of elements and incomprehensible names, looks like AI is generating the code according to principles only known to oneself 🙂

Also I could not find proper place to gain advanced knowledge about how to work with UI elements. I will be very grateful for any help with where to look/get information regarding how the UI elements really work. The original descriptions are not saying anything specific and especially in the section I would need.

 

 

VJR
Super User
Super User

Hi @Mus 

 

There are several videos on the topic and you can start by taking a look at the below ones.

https://www.youtube.com/watch?v=nbSNXebuiCM

https://www.youtube.com/watch?v=0pNj8c-lcQ0

 

 

Once you are done going through and followed the same, do post back if there are any issues in clicking the button with screenshots.

 






-------------------------------------------
Help fellow beings find relevant posts.
Upvote to a supporting response.
Mark as solution to an answer.

Mus
Regular Visitor

Hi @VJR,

 

Thanks for the links to videos, I have already saw these and unfortunately I need more advanced tips than these.

The idea is, that page I would like to run automation on, is dynamic. The Item/button names are the same, but JS path and selectors are different each time if using menu buttons. I don't know how to solve this properly with PA UI in such environment. This was the reason I would like to use JavaScript instead, as by script I can locate the Item I need to click or fill with text quite simple, but then, PA is not running the script on the page.

 

The UI elements with static page is quite simple, like on the videos, but if needed complicated solution, there is lack of proper and advanced information how it works, how it reflects the path I can get from page by Inspect the element and how to use it on UI element.

 

Just as example, let's take an element from one local page, element is a simple button/link:

1. Element path in page I can get through Inspect:

  1.1. Selector: #quick-site-section-navigation-inner > a:nth-child(5) > span

  1.2. JS Path: document.querySelector("#quick-site-section-navigation-inner > a:nth-child(5) > span")

  1.3. XPath: //*[@id="quick-site-section-navigation-inner"]/a[4]/span

  1.4. XPath full: /html/body/header/div[2]/div[1]/div/div[1]/nav/a[4]/span

2. PA UI Selector if I click the element from PA UI menu:

  2.1. UI Element: nav[Id="quick-site-section-navigation-inner"] > a[Class="nav-link text-pale-sky notranslate p-2 px-overwrite-4 "]:eq(3) > span

 

Yes, I can see some similarities and could guess what is what in simple situations, but if I need to write the UI Selector code in section 2 from info I can get from section 1, I can't! And this is just a simplest example.

 

==

And now regarding solution I have using now. this is kind of a workaround until I will found something more solid.

I have created flow with following logic - each time I open a page as a new page, by this making sure that elements are generated with same address/location. I don't use buttons, but opening new page each time it is necessary.

Finding the first element and after I'm building the flow with Send Keys from keyboard.

 

Sounds embarrassing, but at least it works so far 🙂

Anonymous
Not applicable

Hello @Mus 

 

Have you ever tried "Custom" switch located at left bottom corner of the "Selector builder" dialog window?

 

shindomo_0-1632844157841.png

 

You can freely write your own selector code in Custom mode.

 

shindomo_1-1632844198470.png

 

Thank  you.

VJR
Super User
Super User

Hi @Mus 

 

try the suggestion given by @Anonymous 

You might want to share some screenshots of what you are expecting and what it is showing up.

 

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Top Solution Authors
Top Kudoed Authors
Users online (1,873)