Kumar Rishabh
Kumar Rishabh

Reputation: 405

Need to get the Process id of IEDriverServer.exe so that I can fetch the child PIDs for browser

The problem is that I need to get the PID of IE browser instances so that I can close the IE browser(Working in C#). I launched the IE browser using Selenium and then used Driver Service class as :-

InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService();
        Console.WriteLine(driverdetails.Port);

The plan is to get the port and then have its child process. I am able to do so using a debugger by entering the value of Port manually. But, the port fetched by driverdetails.Port was not the actual port used by my driver.

Is there any was, I can find the Port for any given driver service?

For IE i have an alternative to launch IE and get the URL with port which says http://localhost:. However, this is not the case with other browsers. My want is to make the generic code and hence I am using the Driver Service object.

Upvotes: 0

Views: 390

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21408

As far as I know, the InternetExplorerDriverService's ProcessID property gets the process ID of the running driver service executable, and we can't get the IE browser instance PID through the InternetExplorer webdriver. If you want to get the PID, you could try to use the Process class.

From your description, it seems that you want to close the IE tab or window by using the IE Webdriver. If that is the case, I suggest you could use InternetExplorerDriver WindowHandles to get the opened windows, then use the switchto method to switch the window and check the url or title, finally, call the Close method to close the IE window. Please refer to the following sample code:

    private const string URL = @"https://dillion132.github.io/login.html";
    private const string IE_DRIVER_PATH = @"D:\Downloads\webdriver\IEDriverServer_x64_3.14.0";  // where the Selenium IE webdriver EXE is.
    static void Main(string[] args)
    { 
        InternetExplorerOptions opts2 = new InternetExplorerOptions() { InitialBrowserUrl = "https://www.bing.com", IntroduceInstabilityByIgnoringProtectedModeSettings = true, IgnoreZoomLevel = true };
        using (var driver = new InternetExplorerDriver(IE_DRIVER_PATH, opts2))
        {
            driver.Navigate(); 
            Thread.Sleep(5000);
            //execute javascript script
            var element = driver.FindElementById("sb_form_q");
            var script = "document.getElementById('sb_form_q').value = 'webdriver'; console.log('webdriver')";
            IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
            jse.ExecuteScript(script, element);


            InternetExplorerDriverService driverdetails = InternetExplorerDriverService.CreateDefaultService(IE_DRIVER_PATH);
            Console.WriteLine(driverdetails.Port);

            // open multiple IE windows using webdriver.
            string url = "https://www.google.com/";
            string javaScript = "window.open('" + url + "','_blank');";

            IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)driver;
            jsExecutor.ExecuteScript(javaScript);

            Thread.Sleep(5000);

            //get all opened windows (by using IE Webdriver )
            var windowlist = driver.WindowHandles;
            Console.WriteLine(windowlist.Count);

            //loop through the list and switchto the window, and then check the url 
            if(windowlist.Count > 1)
            { 
                foreach (var item in windowlist)
                {
                    driver.SwitchTo().Window(item);
                    Console.WriteLine(driver.Url);

                    if(driver.Url.Contains("https://www.bing.com"))
                    {
                        driver.Close(); //use the Close method to close the window. The Quit method will close the browser window and dispose the webdriver.

                    }

                }
            }

            Console.ReadKey();
        }

        Console.ReadKey();

    }

Upvotes: 1

Related Questions