加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

在Windows窗体中嵌入Selenium ChromeDriver

发布时间:2020-12-14 04:21:22 所属栏目:Windows 来源:网络整理
导读:我想将Selenium ChromeDriver嵌入 Windows窗体面板中.我已经成功地遵循 this example在Windows窗体中嵌入记事本.我认为我遇到问题的问题是从ChromeDriver获取MainWindowHandle.这是我到目前为止所得到的:(完整代码的粘贴链接: https://pastebin.com/RNv2vbJ
我想将Selenium ChromeDriver嵌入 Windows窗体面板中.我已经成功地遵循 this example在Windows窗体中嵌入记事本.我认为我遇到问题的问题是从ChromeDriver获取MainWindowHandle.这是我到目前为止所得到的:(完整代码的粘贴链接: https://pastebin.com/RNv2vbJ1)
//----------------------TEST---------------------
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;

ChromeDriver driver = new ChromeDriver(service);
Console.WriteLine(driver.CurrentWindowHandle);

Process chromeDriverProcess = Process.GetProcessById(service.ProcessId);
//EmbeddedWindowHandle = driver.CurrentWindowHandle;
//----------------------TEST---------------------

ProcessStartInfo processInfo = new ProcessStartInfo("notepad.exe");
Process p = Process.Start(processInfo);
p.WaitForInputIdle();
EmbeddedWindowHandle = p.MainWindowHandle;
SetParent(EmbeddedWindowHandle,panel1.Handle);
MoveWindow(EmbeddedWindowHandle,panel1.Width,panel1.Height,true);
SetWindowLong(EmbeddedWindowHandle,GWL_STYLE,WS_VISIBLE);
所以嵌入一个控制台应用程序有点类似于你的方法,但它不一样.我发现很多线程说它不可能或者它不起作用

How to embed a Console application inside a Winforms application

Embedding a DOS console in a windows form

Embedding a DOS console in a windows form with Visual Basic

并且以下链接可以创建您自己的控件作为CMD

https://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application

但我终于设法让下面的代码工作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private IntPtr cmdPtr;
        private int ConsoleWidth;
        private IntPtr folderViewPtr;

        private const int SWP_NOOWNERZORDER = 0x200;
        private const int SWP_NOREDRAW = 0x8;
        private const int SWP_NOZORDER = 0x4;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int WS_EX_MDICHILD = 0x40;
        private const int SWP_FRAMECHANGED = 0x20;
        private const int SWP_NOACTIVATE = 0x10;
        private const int SWP_ASYNCWINDOWPOS = 0x4000;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = (-20);
        private const int WS_VISIBLE = 0x10000000;
        private const int WM_CLOSE = 0x10;
        private const int WS_CHILD = 0x40000000;

        public abstract class WS
        {
            public const uint WS_OVERLAPPED = 0x00000000;
            public const uint WS_POPUP = 0x80000000;
            public const uint WS_CHILD = 0x40000000;
            public const uint WS_MINIMIZE = 0x20000000;
            public const uint WS_VISIBLE = 0x10000000;
            public const uint WS_DISABLED = 0x08000000;
            public const uint WS_CLIPSIBLINGS = 0x04000000;
            public const uint WS_CLIPCHILDREN = 0x02000000;
            public const uint WS_MAXIMIZE = 0x01000000;
            public const uint WS_CAPTION = 0x00C00000;     /* WS_BORDER | WS_DLGFRAME  */
            public const uint WS_BORDER = 0x00800000;
            public const uint WS_DLGFRAME = 0x00400000;
            public const uint WS_VSCROLL = 0x00200000;
            public const uint WS_HSCROLL = 0x00100000;
            public const uint WS_SYSMENU = 0x00080000;
            public const uint WS_THICKFRAME = 0x00040000;
            public const uint WS_GROUP = 0x00020000;
            public const uint WS_TABSTOP = 0x00010000;

            public const uint WS_MINIMIZEBOX = 0x00020000;
            public const uint WS_MAXIMIZEBOX = 0x00010000;

            public const uint WS_TILED = WS_OVERLAPPED;
            public const uint WS_ICONIC = WS_MINIMIZE;
            public const uint WS_SIZEBOX = WS_THICKFRAME;
            public const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW;

            // Common Window Styles

            public const uint WS_OVERLAPPEDWINDOW =
                (WS_OVERLAPPED |
                  WS_CAPTION |
                  WS_SYSMENU |
                  WS_THICKFRAME |
                  WS_MINIMIZEBOX |
                  WS_MAXIMIZEBOX);

            public const uint WS_POPUPWINDOW =
                (WS_POPUP |
                  WS_BORDER |
                  WS_SYSMENU);

            public const uint WS_CHILDWINDOW = WS_CHILD;

            //Extended Window Styles

            public const uint WS_EX_DLGMODALFRAME = 0x00000001;
            public const uint WS_EX_NOPARENTNOTIFY = 0x00000004;
            public const uint WS_EX_TOPMOST = 0x00000008;
            public const uint WS_EX_ACCEPTFILES = 0x00000010;
            public const uint WS_EX_TRANSPARENT = 0x00000020;

            //#if(WINVER >= 0x0400)
            public const uint WS_EX_MDICHILD = 0x00000040;
            public const uint WS_EX_TOOLWINDOW = 0x00000080;
            public const uint WS_EX_WINDOWEDGE = 0x00000100;
            public const uint WS_EX_CLIENTEDGE = 0x00000200;
            public const uint WS_EX_CONTEXTHELP = 0x00000400;

            public const uint WS_EX_RIGHT = 0x00001000;
            public const uint WS_EX_LEFT = 0x00000000;
            public const uint WS_EX_RTLREADING = 0x00002000;
            public const uint WS_EX_LTRREADING = 0x00000000;
            public const uint WS_EX_LEFTSCROLLBAR = 0x00004000;
            public const uint WS_EX_RIGHTSCROLLBAR = 0x00000000;

            public const uint WS_EX_CONTROLPARENT = 0x00010000;
            public const uint WS_EX_STATICEDGE = 0x00020000;
            public const uint WS_EX_APPWINDOW = 0x00040000;

            public const uint WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
            public const uint WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
            //#endif /* WINVER >= 0x0400 */

            //#if(_WIN32_WINNT >= 0x0500)
            public const uint WS_EX_LAYERED = 0x00080000;
            //#endif /* _WIN32_WINNT >= 0x0500 */

            //#if(WINVER >= 0x0500)
            public const uint WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children
            public const uint WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
                                                            //#endif /* WINVER >= 0x0500 */

            //#if(_WIN32_WINNT >= 0x0500)
            public const uint WS_EX_COMPOSITED = 0x02000000;
            public const uint WS_EX_NOACTIVATE = 0x08000000;
            //#endif /* _WIN32_WINNT >= 0x0500 */
        }



        public Form1()
        {
            InitializeComponent();
            this.Width = 700;
            this.ConsoleWidth = this.Width;
            folderViewPtr = FindWindow("Progman",null);
            folderViewPtr = FindWindowEx(folderViewPtr,IntPtr.Zero,"SHELLDLL_DefView","SysListView32","FolderView");

            //SetWindowLong(this.Handle,-8,(int)folderViewPtr); //GWL_HWNDPARENT

            Process p = Process.Start("cmd.exe","/k cd %userprofile%desktop");
            Thread.Sleep(500); // Allow the process to open it's window
            cmdPtr = p.MainWindowHandle;
            SetParent(cmdPtr,this.Handle);

            SetWindowLong(this.Handle,-20,(int)(GetWindowLong(this.Handle,-20) | WS.WS_CLIPCHILDREN ));

            // Remove border and whatnot
            SetWindowLong(cmdPtr,(int)(WS.WS_VISIBLE ));

            long exStyle = GetWindowLong(p.MainWindowHandle,GWL_EXSTYLE);
            exStyle &= ~(WS.WS_EX_APPWINDOW | WS.WS_EX_CLIENTEDGE);


            SetWindowLong(p.MainWindowHandle,GWL_EXSTYLE,(int)0);

            MoveWindow(cmdPtr,-2,this.ConsoleWidth,this.Height,true);

            SetWindowPos(this.Handle,(IntPtr)1,this.Left,this.Top,this.Width,0);

            timer1.Start();
        }


        protected override void WndProc(ref Message message)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;
            const int WM_WINDOWPOSCHANGING = 0x0046;

            switch (message.Msg)
            {
                case WM_SYSCOMMAND:
                    int command = message.WParam.ToInt32() & 0xfff0;
                    if (command == SC_MOVE)
                    {
                        //return;
                    }
                    break;

                case WM_WINDOWPOSCHANGING:
                    SetWindowPos(this.Handle,0x0004);
                    Debug.Print("WM_WINDOWPOSCHANGING");
                    break;
            }

            base.WndProc(ref message);
        }

        protected override void OnResize(EventArgs e)
        {
            if (this.cmdPtr != IntPtr.Zero)
            {
                MoveWindow(cmdPtr,true);
            }
            base.OnResize(e);
        }


        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (cmdPtr != IntPtr.Zero)
            {
                PostMessage(cmdPtr,0x0010,0);
            }
            base.OnHandleDestroyed(e);
        }

        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild,IntPtr hWndNewParent);

        [DllImport("user32.dll",SetLastError = true)]
        static extern UInt32 GetWindowLong(IntPtr hWnd,int nIndex);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd,int nIndex,int dwNewLong);

        [DllImport("User32.dll")]
        static extern bool MoveWindow(IntPtr hWnd,int X,int Y,int nWidth,int nHeight,bool repaint);

        [DllImport("User32.dll")]
        static extern bool PostMessage(IntPtr hWnd,uint message,int wparam,int lparam);

        [DllImport("User32.dll")]
        static extern void SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern void SetWindowPos(IntPtr hWnd,IntPtr hWndNew,int x,int y,int cx,int cy,UInt32 uFlags);

        [DllImport("user32.dll")]
        static extern IntPtr GetActiveWindow();

        [DllImport("user32.dll")]
        static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(int xPoint,int yPoint);

        [DllImport("user32.dll",CharSet = CharSet.Auto,SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd,StringBuilder lpString,int nMaxCount);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hWnd,IntPtr hWndA,string lpClassName,string lpWindowName);

        private void Form1_Load(object sender,EventArgs e)
        {
            this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right/2 - this.Width/2;
            this.Top = 0;
        }

        private void timer1_Tick(object sender,EventArgs e)
        {
            //check if cmd still exists
            if (!IsWindow(cmdPtr))
            {
                Application.Exit();
            }
        }

    }

}

PS:积分到https://github.com/KillPinguin/CommandLineWidget

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读