WebBrowser control does not respond to InvokeMember ("click")

After spending 5 days in my life, I am going to abandon this, but first of all I advise experts.

I have a WebBrowser control that loads a web page and I programmatically clear its contents. When you click on a specific menu item on the page, the "Open File" dialog box opens when it is executed in IE (or any other browser). But pressing the same button in a WebBrowser control with InvokeMember(), apparently, does nothing no matter what. I looked at several SO issues, such as Configuring Browser Settings , to make sure my control behaves exactly the same as IE, but it failed.

I got to checking the actual javascript function that the button executes behind the scene and calls it manually using HtmlDocument.InvokeScript(), but cannot do, because the base function accepts an argument of type MouseEvent(actually click), and I'm not sure how to create this object on WITH#.

Another approach was to set focus on that particular button, and then try SendKeys, but that would not work, because the WebBrowser control is not displayed. This is just an instance in mind. To be more specific, WebBrowser

EDIT

In a reader request, here is a simple code that I use to search for an element:

var MyButton = WB.Document.GetElementById("processfilelink");

processfilelink (<a href='#' ... >), , . - jQuery delegate click . InvokeMember() :

MyButton.InvokeMember("click");

. mousedown, mouseup focus . , , click, InvokeMember . .

+4
1

:

... IE, F12 Tools button.click() JavaScript. , ?

, , :

... . ! . ?

, , MyButton.InvokeMember("click") . -, , onclick. , onmousedown onmouseup. , , F12 .

, , onmousedown/onmouseup, WebBrowser , 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);
    }
}
+3

Source: https://habr.com/ru/post/1535863/


All Articles