• c# webbrowser控件 获取位置并且模拟鼠标点击


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using mshtml;
    
    namespace baidu
    {
    public partial class Form1 : Form
    {
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
      static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
      [DllImport("user32.dll", SetLastError = true)]
      static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
    
      [DllImport("user32.dll", CharSet = CharSet.Auto)]
      static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
    
      HtmlDocument htmlDocument;
    
      public Form1()
      {
        InitializeComponent();
      }
    
      [DllImport("user32")]
      public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
    
    
       /*
                 获取位置
          */
        
      public void location()
      {
        htmlDocument.GetElementById("kw").SetAttribute("value", "nihao");
    
        Point p = new Point();
    
      HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
    
      IHTMLElementCollection els = doc.getElementsByTagName("INPUT");
    
      foreach (IHTMLElement em in els)
      {
    
        // URL www.baidu.com
        if (em.getAttribute("value") == "百度一下")
        {
          IHTMLElement pem = em;
          //元素中间
          p.X = em.offsetWidth / 2;
          //MessageBox.Show(p.X.ToString());
          p.Y = em.offsetHeight / 2;
          //MessageBox.Show(p.Y.ToString());
          do
          {
            pem = pem.offsetParent;
            p.X += pem.offsetLeft; 
            p.Y += pem.offsetTop;
          } while (pem.tagName.ToLower() != "body");
          //MessageBox.Show(p.X.ToString());
          //MessageBox.Show(p.Y.ToString());
          em.scrollIntoView();//显示元素
          break;
        }
      }
    }
    
    
    
           /*
                 点击
          */
      public void button2_Click()
      {
        htmlDocument.GetElementById("kw").SetAttribute("value", "success");
    
        // 这里是location方法 获取的位置
        int x = 789; // X coordinate of the click 
        int y = 238; // Y coordinate of the click 
        IntPtr handle = webBrowser1.Handle;
        StringBuilder className = new StringBuilder(789);
        while (className.ToString() != "Internet Explorer_Server") // The class control for the browser 
        {
          handle = GetWindow(handle, 5); // Get a handle to the child window 
          GetClassName(handle, className, className.Capacity);
        }
    
        IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates 
        IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl) 
        const uint downCode = 0x201; // Left click down code 
        const uint upCode = 0x202; // Left click up code 
        SendMessage(handle, downCode, wParam, lParam); // Mouse button down 
        SendMessage(handle, upCode, wParam, lParam); // Mouse button up 
    
      }
    
      private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
        this.htmlDocument = webBrowser1.Document;
    
        //location();
    
        button2_Click();
    
      }
    }
    }
    
    //这算是我从别的地方获取并总结出来的,希望能帮助大家。
    

      

  • 相关阅读:
    python_tkinter弹出对话框2
    python_tkinter弹出对话框1
    python生成图片二维码(利用pillow)
    nginx配置ssl证书流程及常见问题
    Django app安装,配置mysql,时区,模板,静态文件,媒体,admin
    使用Git Flow规范!
    python快速生成验证码
    json&pickle模块
    sys模块
    常用模块
  • 原文地址:https://www.cnblogs.com/pasijiubiehuozhe/p/6165078.html
Copyright © 2020-2023  润新知