WebBrowser control not responding to InvokeMember(“click”)

后端 未结 1 1814
醉酒成梦
醉酒成梦 2021-02-10 17:22

After spending 5 days of my life, I\'m about to give up on this, but am consulting the experts once before that.

I have a WebBrowser control that loads a webpage and I p

相关标签:
1条回答
  • 2021-02-10 17:26

    From the comments:

    ... load this page in full IE browser, use F12 Tools to debug it and execute button.click() in JavaScript console. Does it work as expected this way?

    So, you've tried that, and the result is:

    ... now that's interesting. It doesn't work! But clicking on the item by hand does work flawlessly. What's going on here?

    I suspected that as MyButton.InvokeMember("click") doesn't work. Apparently, the page handles this click by other means than via onclick event. Most likely, it uses onmousedown or onmouseup events. Study the page's scripting logic to verify if that's the case, use F12 debugger and put some break points.

    Updated, if it turns out the page indeed uses onmousedown/onmouseup, you'd need to make your WebBrowser visible and automate it by posting WM_LBUTTONDOWN:

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication_22979038
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void MainForm_Load(object sender, EventArgs e)
            {
                this.webBrowser.DocumentText = "<a id='goLink' href='javascript:alert(\"Hello!\"),undefined'>Go</a><script></script>";
    
                this.webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
            }
    
            void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                var element = this.webBrowser.Document.GetElementById("goLink");
                element.Focus();
                var hwnd = GetFocus();
                if (!IsChild(this.webBrowser.Handle, hwnd))
                    throw new ApplicationException("Unexpected focused window.");
    
                var rect = GetElementRect(element);
                IntPtr wParam = (IntPtr)MK_LBUTTON;
                IntPtr lParam = (IntPtr)(rect.Left | rect.Top << 16);
                PostMessage(hwnd, WM_LBUTTONDOWN, wParam, lParam);
                PostMessage(hwnd, WM_LBUTTONUP, wParam, lParam);
            }
    
            // get the element rect in window client area coordinates
            static Rectangle GetElementRect(HtmlElement element)
            {
                var rect = element.OffsetRectangle;
                int left = 0, top = 0;
                var parent = element;
                while (true)
                {
                    parent = parent.OffsetParent;
                    if (parent == null)
                        return new Rectangle(rect.X + left, rect.Y + top, rect.Width, rect.Height);
                    var parentRect = parent.OffsetRectangle;
                    left += parentRect.Left;
                    top += parentRect.Top;
                }
            }
    
            // interop
    
            const int MK_LBUTTON = 0x0001;
            const int WM_LBUTTONDOWN = 0x0201;
            const int WM_LBUTTONUP = 0x0202;
    
            [StructLayout(LayoutKind.Sequential)]
            public struct POINT
            {
                public int x;
                public int y;
            }
    
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            static extern int PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    
            [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
            static extern IntPtr GetFocus();
    
            [DllImport("User32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
            static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);
        }
    }
    
    0 讨论(0)
提交回复
热议问题