Selenium WebDriver – Get Browser Hwnd

While designing some UI automation framework API, which can work with browser and other systems like window, I have to control the z order or IExplore launched by selenium web driver.

 

However out of box, selenium does not expose driver process id or Browser hwnd.

The attached solution with some extensions is able to get the browser hwnd..

The basic logic is

  • 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
Below is some code highlights
// Interface to extend the web driver and expose ProcessId of driver
public interface IWebDriverEx: IWebDriver
{
/// <summary>
/// Get the process id of driver
/// </summary>
/// <returns>process id</returns>
int ProcessId { get; }
}
and few extension methods
public static IntPtr GetBrowserHwnd(this IWebDriverEx driver);
public static void BringToFront(this IWebDriverEx driver);
for more details, see attached sample (download), after launch go to console and press any key to bring the browser in front…

10 thoughts on “Selenium WebDriver – Get Browser Hwnd

  1. I would love to see the code for this but the link does not work. Can you either fix this or just post the code directly onto the blog? Here is the error:

    Sorry, Word Online ran into a problem opening this document. To view this document please open it in Microsoft Word.

    When I download and open with actual Microsoft Word I get error:

    We’re sorry. We can’t open SeleniumBrowserHwnd.docx because we found a problem with its contents.

    Please fix. I am struggling to find my own method to expose browser hwnd with selenium.

    Like

  2. I would love to see the code for this but the link does not work. Can you either fix this or just post the code directly onto the blog? Here is the error:

    Sorry, Word Online ran into a problem opening this document. To view this document please open it in Microsoft Word.

    When I download and open with actual Microsoft Word I get error:

    We’re sorry. We can’t open SeleniumBrowserHwnd.docx because we found a problem with its contents.

    Please fix. I am struggling to find my own method to expose browser hwnd with selenium.

    Like

  3. I would love to see the code for this but the link does not work. Can you either fix this or just post the code directly onto the blog? Here is the error:

    Sorry, Word Online ran into a problem opening this document. To view this document please open it in Microsoft Word.

    When I download and open with actual Microsoft Word I get error:

    We’re sorry. We can’t open SeleniumBrowserHwnd.docx because we found a problem with its contents.

    Please fix. I am struggling to find my own method to expose browser hwnd with selenium.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s