web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Javascript won't execu...
Power Automate
Unanswered

Javascript won't execute on web page

(1) ShareShare
ReportReport
Posted on by 6

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?

  • VJR Profile Picture
    7,635 on at

    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 Profile Picture
    6 on at

    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 Profile Picture
    7,635 on at

    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 Profile Picture
    6 on at

    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 🙂

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    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 Profile Picture
    7,635 on at

    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.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 355

#2
11manish Profile Picture

11manish 336 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 308

Last 30 days Overall leaderboard