以下问题:
- 我需要模拟一个 div 元素的双击。
- 我需要使用/处理 MS Internet Explorer 11。
- 我无法更改目标站点的源代码。
到目前为止我的解决方案:
- 我使用 C# 和 SHDocVw.InternetExplorer 和 mshtml
- 我可以触发“点击”事件,它们会出现在所有处理程序中
- 尝试模拟双击时:
-
- “ondoubleclick”事件被触发。
-
el.addEventListener('dblclick', handler);
不会被触发
为了降低实际问题站点的复杂性,我也创建了这个模拟问题的 HTML 代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>csi dbclick event handler</title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<br>
<div
id="baba"
style="position: absolute; left: 80px; width: 100px; border: 2px solid green;"
onclick="console.log('onclick');"
ondblclick="console.log('ondblclick');"
>
GOODBYE!
</div>
</body>
<script>
function printEvent(pEvent1, pEvent2, pEvent3) {
console.log("New Event: " + pEvent1);
console.log(pEvent1);
console.log("Timestamp: " + pEvent1.timeStamp);
}
const el = document.getElementById('baba');
el.addEventListener('click', printEvent);
el.addEventListener('dblclick', printEvent);
</script>
</html>
我的 C# 代码是这样的:
InternetExplorer ie = new InternetExplorer();
HTMLDocument doc = (HTMLDocument) ie.Document;
IHTMLElement el = doc.getElementById("baba");
el.click();
Thread.Sleep(130);
el.click();
Thread.Sleep(5);
IHTMLEventObj eo3 = new MyEvent(el, "dblclick"); // class that extends IHTMLObject, returns values like a mouse click I logged from IE11 earlier, only type is adjusted
IHTMLElement3 el3 = (IHTMLElement3)el;
el3.FireEvent("ondblclick", eo3);
和我的事件类(可能根本不需要):
class MyEvent : IHTMLEventObj {
private readonly IHTMLElement mElement;
private readonly String mType;
public MyEvent(IHTMLElement pElement, String pType) {
mElement = pElement;
mType = pType;
}
bool IHTMLEventObj.altKey { get { return false; } }
int IHTMLEventObj.button { get { return 0; } }
bool IHTMLEventObj.cancelBubble { get { return false; } set { } }
int IHTMLEventObj.clientX { get { return 85; } }
int IHTMLEventObj.clientY { get { return 130; } }
bool IHTMLEventObj.ctrlKey { get { return false; } }
IHTMLElement IHTMLEventObj.fromElement { get { return null; } }
int IHTMLEventObj.keyCode { get { return 0; } set { } }
int IHTMLEventObj.offsetX { get { return 3; } }
int IHTMLEventObj.offsetY { get { return 7; } }
string IHTMLEventObj.qualifier { get { return "baba"; } }
int IHTMLEventObj.reason { get { return 0; } }
object IHTMLEventObj.returnValue { get { return null; } set { } }
int IHTMLEventObj.screenX { get { return 353; } }
int IHTMLEventObj.screenY { get { return 305; } }
bool IHTMLEventObj.shiftKey { get { return false; } }
IHTMLElement IHTMLEventObj.srcElement { get { return mElement; } }
object IHTMLEventObj.srcFilter { get { return mElement; } }
IHTMLElement IHTMLEventObj.toElement { get { return mElement; } }
string IHTMLEventObj.type { get { return mType; } }
int IHTMLEventObj.x { get { return 85; } }
int IHTMLEventObj.y { get { return 127; } }
}
运行代码时,我得到以下结果:
onclick
New Event: [object PointerEvent]
[object PointerEvent]
{
[functions]: ,
__proto__: { },
altKey: false,
AT_TARGET: 2,
bubbles: true,
BUBBLING_PHASE: 3,
button: 0,
buttons: 0,
cancelable: true,
cancelBubble: false,
CAPTURING_PHASE: 1,
clientX: -396.3999938964844,
clientY: -146.84999084472656,
constructor: { },
ctrlKey: false,
currentTarget: { },
defaultPrevented: false,
detail: 0,
deviceSessionId: 0,
eventPhase: 2,
fromElement: null,
height: 0,
hwTimestamp: 0,
isPrimary: false,
isTrusted: true,
layerX: -396.3999938964844,
layerY: -146.84999084472656,
metaKey: false,
offsetX: -478.3999938964844,
offsetY: -267.25,
pageX: -396.3999938964844,
pageY: -146.84999084472656,
pointerId: 0,
pointerType: "",
pressure: 0,
relatedTarget: null,
rotation: 0,
screenX: 0,
screenY: 0,
shiftKey: false,
srcElement: { },
target: { },
tiltX: 0,
tiltY: 0,
timeStamp: 1612115536885,
toElement: null,
type: "click",
view: { },
which: 1,
width: 0,
x: -396.3999938964844,
y: -146.84999084472656
}
Timestamp: 1612115536885
onclick
New Event: [object PointerEvent]
[object PointerEvent]
{
[functions]: ,
__proto__: { },
altKey: false,
AT_TARGET: 2,
bubbles: true,
BUBBLING_PHASE: 3,
button: 0,
buttons: 0,
cancelable: true,
cancelBubble: false,
CAPTURING_PHASE: 1,
clientX: -396.3999938964844,
clientY: -146.84999084472656,
constructor: { },
ctrlKey: false,
currentTarget: { },
defaultPrevented: false,
detail: 0,
deviceSessionId: 0,
eventPhase: 2,
fromElement: null,
height: 0,
hwTimestamp: 0,
isPrimary: false,
isTrusted: true,
layerX: -396.3999938964844,
layerY: -146.84999084472656,
metaKey: false,
offsetX: -478.3999938964844,
offsetY: -267.25,
pageX: -396.3999938964844,
pageY: -146.84999084472656,
pointerId: 0,
pointerType: "",
pressure: 0,
relatedTarget: null,
rotation: 0,
screenX: 0,
screenY: 0,
shiftKey: false,
srcElement: { },
target: { },
tiltX: 0,
tiltY: 0,
timeStamp: 1612115537021,
toElement: null,
type: "click",
view: { },
which: 1,
width: 0,
x: -396.3999938964844,
y: -146.84999084472656
}
Timestamp: 1612115537021
ondblclick
如您所见,点击事件都到达(onclick, click, onclick, click, ondblclick)
了,但addEventListener('dblclick', ...)
没有。应该(onclick, click, onclick, click, ondblclick, dblclick)
在最后。有什么想法可以做到这一点吗?
我的另一个解决方案是劫持鼠标并模拟“真正的”点击,但这对我来说太老套了。