当前位置:网站首页>c# 坐标点击webBrowser1
c# 坐标点击webBrowser1
2022-04-21 15:50:00 【程序007】
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();
}
}
}
//这算是我从别的地方获取并总结出来的,希望能帮助大家。
版权声明
本文为[程序007]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_36664772/article/details/115745604
边栏推荐
- AcWing1800. Do not do the last (enumeration)
- AnalyticDB PostgreSQL新版云原生让用户专注于业务,实现怎样的体验升级?
- Latex common formula query
- Sed command
- 程序员美团面试经历,从基础到算法历时六个小时面试,年薪20w
- ABAP keywords at first and at last learning
- 季更56/90
- Why are you so tired to be a programmer?
- R语言广义线性模型函数GLM、广义线性模型(Generalized linear models)、glm函数构建逻辑回归模型(Logistic regression)
- MySQL-IF-THEN-ELSE语句
猜你喜欢

外包学生管理系统架构方案

回调函数
程序员美团面试经历,从基础到算法历时六个小时面试,年薪20w

mysql数据库字段上下移动

Ultimate doll 2.0 | observable practice sharing of cloud native PAAS platform

提取CNN模型中间层输出方法

ABAQUS loads according to coordinates - analytical field load mapping

Oracle official announcement: Tencent JDK 18 ranks first in China!

ABAP string function list

National Rainfall Erosivity Factor r value
随机推荐
徽商期货开户是否可靠?资金安全吗?
Mark一下,两年365个粉丝
盲盒项目的暴利玩法
R语言广义线性模型函数GLM、广义线性模型(Generalized linear models)、glm函数构建逻辑回归模型(Logistic regression)
AcWing 1812. Square pasture (enumeration)
MySQL-IF-THEN-ELSE语句
SAP Fiori smart template技术里CDS view的注解和UI元素对应关系
Is the account opening of Huishang futures company reliable? Is the transaction safe?
OpenCV 4.0学习笔记
干货 | 解决 App 自动化测试的常见痛点(弹框及首页启动加载完成判断处理)
(计数线段树)lintcode中等248 · 统计比给定整数小的数的个数
LeetCode刷题之652寻找重复的子树
Detailed explanation of swap automatic arbitrage robot ecosystem development mode
SharkTeam发布2022年Q1智能合约安全态势感知季报
万有导航:简洁实用的综合导航网站
LeetCode 3、无重复字符的最长子串
Ji Geng 56 / 90
AcWing 1866. Fence painting (section crossing)
做程序员为什么这么累?
Spark comprehensive exercise - Analysis of film scoring data