How to get the process id of opened browser in java?

2,906 views
Skip to first unread message

Anto Aravinth

unread,
Jul 17, 2014, 8:49:36 AM7/17/14
to seleniu...@googlegroups.com

Is there any way in which I can get the process id of the launched browser? Is that possible?


I’m using Selenium WebDriver. Test cases are running in mac against Safari browsers.  

Anto Aravinth

unread,
Jul 18, 2014, 4:06:08 AM7/18/14
to seleniu...@googlegroups.com
Any reply on this?

Thanks.

Eric Chen

unread,
Jul 18, 2014, 4:24:27 AM7/18/14
to seleniu...@googlegroups.com
you can just use a stack to track all the window handles, and when you know there will be a new handle you can call WebDriver.getWindowHandles(), and use the last group to minus the stack group, you will get the newly created handle, right?

Jianfeng Sun

unread,
Jul 20, 2014, 12:14:52 AM7/20/14
to seleniu...@googlegroups.com
1) wd.toString();
2) System.getProperty("os.name");
3) JavascriptExecutor js = (JavascriptExecutor) wd;
    Object ms=js.executeScript("return navigator.userAgent","");
    String value =ms.toString();
Not sure what you want. 

Anto Aravinth

unread,
Jul 21, 2014, 11:45:41 PM7/21/14
to seleniu...@googlegroups.com
This will get the user agent. I want is process id. 

Anto Aravinth

unread,
Jul 21, 2014, 11:48:44 PM7/21/14
to seleniu...@googlegroups.com
Fine. Let me explain my problem:


Upload is not working in Safari. WebDriver can’t able to click on the upload button and also the trick of sendKeys to upload file also not working either. So we wrote an applescript for it. Now, in parallel mode, we want to know which safari needs the upload button click. In that case, the only solution is to get the process id of that browser and then send to applescript so that it picks the right one! This is what we are trying. 

Any idea will be great!

Krishnan Mahadevan

unread,
Jul 22, 2014, 2:55:33 AM7/22/14
to Selenium Users
Anto,

I haven't gotten the time to actually create a working example of what I am suggesting here. 
But on a high level here's what I think should work:

1. Create a method that would leverage Runtime.getRuntime().exec() and run a shell command which will figure out all the set of process ids matching the "Safari" process before the SafariDriver is instantiated.
2. Make a note of the process id.
3. Instantiate the Safari driver and re-run step (1) and find out the new safari browser process.

There are a lot of quirks with this approach, but I hope it lets you get started.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/3bd9ace8-7bf5-430b-91c7-d54fdfb3ed4c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Rajnish Noonia

unread,
Sep 9, 2016, 8:27:05 AM9/9/16
to Selenium Users
not sure about java , done in .net but same approach can be applied to java

Out of box, selenium does not expose driver process id or Browser hwnd but it is possible. Below is the logic to get hwnd

  • When driver is initialized, get the url for hub and extract the port number
  • From port number, find the process id which is using this port for listening, ie. PID of driver
  • After navigation, from all instances of iexplore find the parent PID matches the pid of driver, i.e browser pid.
  • Get the Hwnd of browser pid once browser hwnd is found , you can use win32 api to bring selenium to foreground.

its not possible to post full code here, the full working solution (C#) to bring browser in front is on my blog

http://www.pixytech.com/rajnish/2016/09/selenium-webdriver-get-browser-hwnd/

Reply all
Reply to author
Forward
0 new messages