cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Window Title by PID

HongJooChoi
Staff
Staff
Hi, Community.


Is there any way to find the Window Title by the PID?

The right Window Title is already known and
what I want to do is to find the right PID from the list (as shown below) by looping through the list of PID and comparing Window Title for each.

20739.png


Best regards

------------------------------
HongJoo Choi
------------------------------
5 REPLIES 5

Hi HongJoo,

Utility - General available on DX can be used for this.

------------------------------
Shashank Kumar
DX Integrations Partner Consultant
Blue Prism
Singapore
+6581326707
------------------------------

PabloSarabia
Level 11
Hi @HongJooChoi

You can get it with this line of code:

title = Process.GetProcessById(3016).MainWindowTitle;


In one VBO, you need to add this name space "System.Diagnostics" and change the language to C#


Hope this helps you!

See you in the Community, bye 🙂

------------------------------
Pablo Sarabia
Solution Manager & Architect
Altamira Assets Management
Madrid
------------------------------

diane.sanzone
Level 7
Hi HongJoo,

I agree with Shashank - using the Utility - General is probably your best bet here.  There is an action in that utility called Read Memory Stats which accepts an input collection of [Process Names]. That collection needs to have 2 data points in it, both of type text, named Process Name and PID.  If you put the process name of msedge in your initial values and leave the PID blank, then run the action, it will return a collection (default name [Process Statistics]) that includes the Process Name, PID, Working Set and Virtual Memory.  You can ignore the pieces you don't need and then loop through the [Process Statistics.PID] data point to attach via PID, check for some element you need, then either proceed or detach and loop through until you get to the right one.

One note with this action call is that it will always also return the Idle process with the PID of 0.  I recommend at the top of your loop through the PIDs you include a decision to check if [Process Statistics.PID]=0 and just jump to the end of the loop if so.

I hope this explanation helps. I've used this action a lot when I have similar things where multiple processes are spawned with the same name, so I also have some sample code I can screenshot over to you if needed.

------------------------------
Diane Sanzone
------------------------------

Hi @Diane Sanzone  The opened tabs are only two but in Process Statistics collection there are many rows with PID, Please help me to understand it better. How can I attach to a dynamic window title using this PID feature.



------------------------------
sivaranjani singaravel
Assistance Software Engineer
accenture
Asia/Kolkata
------------------------------

Hi Sivaranjani,

I'll admit I don't understand why, but when you open an Edge or Chrome window it will spawn multiple processes on your machine (as is visible through the details tab of task manager).  Each additional tab in the browser will spawn multiple new processes, and I don't think there's any ratio for it.  In my experience, you don't need to know which processes go with each tab.  For BP purposes you can just loop through each ID and see if it's the right one by checking for a valid element.  It's hard to explain, so I'll attempt to describe the logic flow.

Assuming you know the window title, you can do this pretty easily with a loop around your attach stage:

  1. Utility - General - Read Memory Stats.  
    1. Inputs- Collection named Process Names. That collection needs to have 2 data points in it, both of type text, named Process Name and PID.  You won't have the PID for the input, but you should have the process name (msedge, chrome, etc.) This is going to be a single row collection with an initial value of whichever browser you're using
    2. Outputs - collection (default name [Process Statistics]) that includes the Process Name, PID, Working Set and Virtual Memory.
  2. Loop through the [Process Statistics] collection
  3. Inside the loop, have an attach stage where your inputs to the attach are (at minimum)
    1. PID - [Process Statistics.PID]
    2. Window Title - {your known window title here}
  4. Put a block around the attach stage to recover any exception (mismatch of PID to window title is expected). Resume and direct back to the end of the loop.  If the attach action completes, direct to the calculation for attached = true and then end the page (you're attached to the correct window)
  5. If you want to further confirm you have the right one before ending, you can also add a wait stage for a known element after the attach is successful and before directing to the calculation stage. This adds minimal processing time but lots of stability.

One thing to note - one of the records returned in the Process Statistics collection will have a PID of 0.  You should add logic to skip that process. It's the "idle" process and doesn't correspond to any browser window.  There are many ways to do that (filter the collection for anything where PID <> 0 to start, check the PID before attempting attach, etc.).  It's up to you how to best complete that part.

I hope this helps clarify. 



------------------------------
Diane Sanzone
------------------------------