Selenium Chrome window running in second monitor screen stealing focus

前端 未结 4 1370
感动是毒
感动是毒 2021-01-01 23:00

I use WebDriver with Chrome Driver 2.35 with Selenium.

I have a dual monitor setup in which I run my test cases. I do run my cases headlessly most of the times but

4条回答
  •  攒了一身酷
    2021-01-01 23:18

    I would suggest setting the window position to be always on bottom. In C#, you can do this through a class that looks like:

    public static class SetWindowPosition
    {
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
        private const UInt32 SWP_NOSIZE = 0x0001;
        private const UInt32 SWP_NOMOVE = 0x0002;
        private const UInt32 SWP_NOACTIVATE = 0x0010;
    
        [DllImport("user32.dll")]
        private static extern bool LockSetForegroundWindow(uint uLockCode);
        private const UInt32 LSFW_LOCK = 1;
    
        public static void ForceWindowToStayOnBottom(Process process)
        {
            SetWindowPos(
                process.MainWindowHandle, // The handle of the browser window
                HWND_BOTTOM, // Tells it the position, in this case the bottom
                0, 0, 0, 0, // Coordinates for sizing of the window - Will be overriden by NOSIZE
                SWP_NOSIZE | // Says to keep the window its current size
                SWP_NOMOVE | // Says to keep the window in its current spot
                SWP_NOACTIVATE // Activation brings the window to the top, we don't want that
            );
    
            // If you don't notice this helping, it can probably be deleted.
            // It only deals with other applications and gets automatically undone on user interaction
            LockSetForegroundWindow(
                LSFW_LOCK); // Locks calls to SetForegroundWindow 
        }
    
        ///  Returns the parent process of each process by the name of processName 
        public static List GetPrimaryProcesses(string processName) =>
    
            Process.GetProcesses() // Gets a list of every process on computer
                .Where(process => process.ProcessName.Contains(processName) // Reduces the list to every process by the name we are looking for
                    && process.MainWindowHandle != IntPtr.Zero) // Removes any process without a MainWindow (which amounts to every child process)
                .ToList();
    }
    

    called as:

    SetWindowPosition.ForceWindowToStayOnBottom(
        SetWindowPosition.GetPrimaryProcesses("chrome")[0]);
    

    where [0] is the index of the window you want to minimize (generally they get returned in the order opened).

    What this does is similar to what you may have done when you set a window to be AlwaysOnTop (try opening task manager, clicking options, and then selecting Always On Top for an example), except in reverse. You should be able to call this on any process you like, including the web driver console window.

    If you want further reading on how this works, these two posts (1, 2) were what I used to get it working, aswell as the MSDN documentation here.

提交回复
热议问题