Get Window handle (IntPtr) from Selenium webdriver's current window GUID

后端 未结 3 1627
野趣味
野趣味 2021-01-17 17:43

I\'m trying to capture a screenshot of whole browser screen (e.g. with any toolbars, panels and so on) not only an entire page, so I\'m got this code:

using          


        
相关标签:
3条回答
  • 2021-01-17 18:17

    You could get the window handle using Process.GetProcesses:

    using (FirefoxDriver driver = new FirefoxDriver())
    {
        driver.Navigate().GoToUrl(url);
    
        string title = String.Format("{0} - Mozilla Firefox", driver.Title);
        var process = Process.GetProcesses()
            .FirstOrDefault(x => x.MainWindowTitle == title);
    
        if (process != null)
        {
            var screenCapture = new ScreenCapture();
            var image = screenCapture.CaptureWindow(process.MainWindowHandle);
            // ...
        }
    }
    

    This of course assumes that you have a single browser instance with that specific title.

    0 讨论(0)
  • 2021-01-17 18:27

    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/

    0 讨论(0)
  • 2021-01-17 18:35

    Just and idea for hack. You may use Reflection methods to get process of firefox instance. First declare FirefoxDriverEx class inherited from FirefoxDriver - to access protected Binary property which encapsulates Process instance:

     class FirefoxDriverEx : FirefoxDriver {
            public Process GetFirefoxProcess() {
                var fi = typeof(FirefoxBinary).GetField("process", BindingFlags.NonPublic | BindingFlags.Instance);
                return fi.GetValue(this.Binary) as Process;
            }
        }
    

    Than you may get process instance for access to MainWindowHandle property

    using (var driver = new FirefoxDriverEx()) {
                driver.Navigate().GoToUrl(url);
    
                var process = driver.GetFirefoxProcess();
                if (process != null) {
                    var screenCapture = new ScreenCapture();
                    var image = screenCapture.CaptureWindow(process.MainWindowHandle);
                    // ...
                }
            }
    
    0 讨论(0)
提交回复
热议问题