1

Web ページから情報を取得するために、Visual Studio でコードを書いています。ここで、html 要素のコンテキスト メニューを開く必要があり、そのようなものを探していました。

webbrowser.Navigate("javascript: document.getElementsByClassName('className')[1].rightclick();void(0);");

残念ながら、Javascript には click() 関数しかないことに気付きました。回避策はありますか?

皆さん、ありがとうございました!

4

2 に答える 2

2

フローステップでこれを行うことができます:

  1. web-browser controlフォーム上のの位置を取得します。

    Point controlLoc = this.PointToScreen(webBrowser1.Location);
    
  2. HtmlElementWeb ブラウザ コントロール上の位置を取得する

    X= element.OffsetRectangle.Left;
    
    Y =element.OffsetRectangle.Top;
    
  3. ポジションを合計します。

    controlLoc.X = controlLoc.X + element.OffsetRectangle.Left;
    
    controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top;
    
  4. マウスの位置を新しい場所に設定します。

    Cursor.Position = controlLoc;
    
  5. マウスの右クリックをシミュレートします。

    MouseSimulator.ClickRightMouseButton();
    

完全なコード:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            webBrowser1.DocumentText = "<button class=\"mybtn\" type=\"submit\"> Right click";
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("button"))
            {
                if (element.GetAttribute("ClassName") == "mybtn")
                {
                    Point controlLoc = this.PointToScreen(webBrowser1.Location);
                    //Get Element Posation
                    controlLoc.X= controlLoc.X + element.OffsetRectangle.Left;
                    controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top;
                    Cursor.Position = controlLoc;
                    MouseSimulator.ClickRightMouseButton();
                }
            }
        }
    }

    public class MouseSimulator
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

        [StructLayout(LayoutKind.Sequential)]
        struct INPUT
        {
            public SendInputEventType type;
            public MouseKeybdhardwareInputUnion mkhi;
        }
        [StructLayout(LayoutKind.Explicit)]
        struct MouseKeybdhardwareInputUnion
        {
            [FieldOffset(0)]
            public MouseInputData mi;

            [FieldOffset(0)]
            public KEYBDINPUT ki;

            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }
        struct MouseInputData
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public MouseEventFlags dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        [Flags]
        enum MouseEventFlags : uint
        {
            MOUSEEVENTF_MOVE = 0x0001,
            MOUSEEVENTF_LEFTDOWN = 0x0002,
            MOUSEEVENTF_LEFTUP = 0x0004,
            MOUSEEVENTF_RIGHTDOWN = 0x0008,
            MOUSEEVENTF_RIGHTUP = 0x0010,
            MOUSEEVENTF_MIDDLEDOWN = 0x0020,
            MOUSEEVENTF_MIDDLEUP = 0x0040,
            MOUSEEVENTF_XDOWN = 0x0080,
            MOUSEEVENTF_XUP = 0x0100,
            MOUSEEVENTF_WHEEL = 0x0800,
            MOUSEEVENTF_VIRTUALDESK = 0x4000,
            MOUSEEVENTF_ABSOLUTE = 0x8000
        }
        enum SendInputEventType : int
        {
            InputMouse,
            InputKeyboard,
            InputHardware
        }

        public static void ClickRightMouseButton()
        {
            INPUT mouseDownInput = new INPUT();
            mouseDownInput.type = SendInputEventType.InputMouse;
            mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN;
            SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));

            INPUT mouseUpInput = new INPUT();
            mouseUpInput.type = SendInputEventType.InputMouse;
            mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP;
            SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
        }
    }

結果:

ここに画像の説明を入力

于 2013-04-20T11:56:28.600 に答える