询问者
webbrowser如何模拟右键点击网页元素?

问题
全部回复
-
你好:
就我所知,你是可以通过执行javascript来执行页面上元素的事件的。
你尝试一下就知道结果。
- 已标记为答案 BoberSongModerator 2010年9月24日 7:13
- 取消答案标记 ThankfulHeartModerator 2014年2月4日 8:45
-
网页上有一个元素没有onclick事件,只有onmousedown,onmouseup事件,怎么在webborwser里模拟右键点击这个元素?
楼主:
首先你必须确保网页中有右键的那个js,比如我的代码(div作为例子):
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script> function DivFun() { document.getElementById("mydiv").oncontextmenu = function () { alert("您点击了右键!"); return false; }; } </script> </head> <body onload="DivFun()"> <div id="mydiv" style="border:groove;border-width:thick;width:100%;height:100%"> <br /> <br /><br /> <br /><br /> <br /><br /> <br /><br /> <br /> </div> </body> </html>
其次,用Windows API把鼠标移动到指定的div区域,模拟右键:
【Form结构】
namespace CSharp { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.webBrowser1 = new System.Windows.Forms.WebBrowser(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // webBrowser1 // this.webBrowser1.Location = new System.Drawing.Point(0, 0); this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); this.webBrowser1.Name = "webBrowser1"; this.webBrowser1.Size = new System.Drawing.Size(704, 321); this.webBrowser1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(296, 342); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(111, 26); this.button1.TabIndex = 1; this.button1.Text = "模拟右键"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(716, 380); this.Controls.Add(this.button1); this.Controls.Add(this.webBrowser1); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.WebBrowser webBrowser1; private System.Windows.Forms.Button button1; } }
【代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace CSharp { public partial class Form1 : Form { [Flags] enum MouseEventFlag : uint { Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, MiddleDown = 0x0020, MiddleUp = 0x0040, XDown = 0x0080, XUp = 0x0100, Wheel = 0x0800, VirtualDesk = 0x4000, Absolute = 0x8000 } /// <summary> /// 定位鼠标和模拟鼠标右键的API /// </summary> /// <param name="flags"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="data"></param> /// <param name="extraInfo"></param> [DllImport("user32.dll")] static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("C:/Users/DongWeiPC/Desktop/Solutions/WebApps/CSharp/HtmlPage1.html"); } private void button1_Click(object sender, EventArgs e) { //移动到div内 mouse_event(MouseEventFlag.Move, 0, -200, 0, new UIntPtr(0)); //Down+Up合并为一次单击 mouse_event(MouseEventFlag.RightDown, 0, 0, 0, new UIntPtr(0)); mouse_event(MouseEventFlag.RightUp, 0, 0, 0, new UIntPtr(0)); } } }
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search -
找了很久,没找到十分好的解决办法,通过API移动光标到相关网页元素,并发送鼠标右键,能触发右键菜单,但那样太麻烦及不够精确。目前只想到了下面的可行的笨方法。
我现在通过不完善的方式,实现了我需要点击右键的功能,思路如下:
先编程找出执行右键菜单时指向的绝对Url,再通过相应信息,组合出要实现的Url,浏览该Url从而获得所需数据。用鼠标触发右键菜单,再通过用定时器执行代码查找Url。
For i = 0 To WebBrowserExt1.Document.Window.Frames.Count - 1
WriteRunLog(WebBrowserExt1.Document.Window.Frames.Item(i).WindowFrameElement.OuterHtml)
WriteRunLog(WebBrowserExt1.Document.Window.Frames.Item(i).WindowFrameElement.Id) '获取Frames的Id值2018.3.7
WriteRunLog(WebBrowserExt1.Document.Window.Frames.Item(i).Url.AbsoluteUri) '获取绝对Url值2018.3.7
Next
WriteRunLog("---------------------------------------------------------------------------------------")
===================请忽略前面的时间戳======
2018/3/7 14:20:56 <IFRAME id=sog src="/devmonitor/abc_right_menu.flowc?dse_sessionId=ISEVJVDXFYDLFVASDGGUIXDXBNDCEPFBJAHODFBD&dse_pageId=0&_flow_context_string=&flowActionName=abc_run_status_op&devId=000000012340180" border=0 frameBorder=0 marginWidth=0 marginHeight=0 style="HEIGHT: 100%; WIDTH: 100%; ></IFRAME>
2018/3/7 14:20:56 sog'下面这行就是执行右键菜单是真正指向的Url,通过浏览该Url就可以获取到所需数据
2018/3/7 14:20:56 http://bifsm.icbc/devmonitor/abc_right_menu.flowc?dse_sessionId=ISEVJVDXFYDLFVASDGGUIXDXBNDCEPFBJAHODFBD&dse_pageId=0&_flow_context_string=&flowActionName=abc_run_status_op&devId=000000012340180
======================
2018/3/7 14:20:59 <IFRAME id=sog src="/devmonitor/abc_right_menu.flowc?dse_sessionId=ISEVJVDXFYDLFVASDGGUIXDXBNDCEPFBJAHODFBD&dse_pageId=0&_flow_context_string=&flowActionName=all_events_jsp&devId=000000012340279" border=0 frameBorder=0 marginWidth=0 marginHeight=0 style="HEIGHT: 100%; WIDTH: 100%; ></IFRAME>
2018/3/7 14:20:59 sog'下面这行是执行右键菜单另一个菜单项的是真正指向的Url,通过浏览该Url就可以获取到所需数据
- 已编辑 zs李四 2018年3月7日 7:34
- 已建议为答案 Hart WangModerator 2018年3月8日 5:34