Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching to specific window using PID (Process ID)

Tags:

java

cmd

runtime

I'm working on a Java Selenium program which uses Runtime class. There are multiple instances of Internet Explorer windows that are open.
I need to "Bring Front" only one specific window for the selenium suite to run without errors.

I have retrieved the specific iexplore instance using tasklist command. Now I have the PID of the process.

Process p = Runtime.getRuntime().exec("tasklist /FI \"WindowTitle eq Google\"");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
String line = null;  
int count=1;
while ((line = in.readLine()) != null) {
    System.out.println(count + " " + line);
    count++;
}

How to bring a process to the front (make it the active window) through Runtime command line using its PID?

EDIT:
Is there a way to switch to an application window from command prompt using the PID of the application?

like image 625
Bharath Avatar asked Nov 26 '25 14:11

Bharath


1 Answers

To switch to an application window from command prompt you can use Windows Script Host's AppActivate function. It accepts either ProcessID or window's title as its argument. Here is a simple script:

set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate Wscript.Arguments(0)

Then call the script by using cscript AppActivate.vbs 1234

like image 137
MBu Avatar answered Nov 29 '25 02:11

MBu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!