我需要在网络浏览器上自动执行重复操作。为此,我需要做一些事情,比如从 java 代码向网页发送点击和其他输入。
Element e = document.getElementById("someid");
com.sun.webpane.webkit.dom.MouseEventImpl.MouseEventImpl mei = MouseImplExpose.makeSimpleClickDummy(e);
((EventTarget)e).dispatchEvent(mei);
MouseImplExpose 在哪里
package com.sun.webpane.webkit.dom;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class MouseImplExpose {
public static MouseEventImpl makeDummy(Node n){
// required jdk 7 update 40 ! This function is
// not available in jdk 7 update 10 :O
return new MouseEventImpl(((NodeImpl)n).peer );
}
public static MouseEventImpl makeSimpleClickDummy(Node n/*, Document d*/){
//d.getD
MouseEventImpl impl = new MouseEventImpl(((NodeImpl)n).peer );
// some fictional values
impl.initMouseEvent(
"click",
true,
true,
null,
1,
958,
318,
718,
208,
false,
false,
false,
false,
(short)0,
(NodeImpl)n);
return impl;
// this is how a real MouseEventImpl generated
// internally looks :
/*evt=com.sun.webpane.webkit.dom.MouseEventImpl@ad3b429
typeArg=click
canBubbleArg=true
cancelableArg=true
viewArg=[object DOMWindow]
detailArg=1
screenXArg=958
screenYArg=318
clientXArg=718
clientYArg=208
ctrlKeyArg=false
altKeyArg=false
shiftKeyArg=false
metaKeyArg=false
buttonArg=0
relatedTargetArg=[object HTMLDivElement]*/
}
}
我从本机端得到一个 org.w3c.dom.events.EventException 。
我相信必须有一个更简单的方法来做到这一点......
任何提示、提示、指南、链接、评论将不胜感激。谢谢
我发现这种替代方法有效
System.out.println(webEngine.executeScript("var evt = document.createEvent(\"MouseEvents\");\n"+
" evt.initMouseEvent(\"click\", true, true, window, 1, 0, 0, 0, 0,\n" +
" false, false, false, false, 0, null);\n" +
"\n" +
" var cb = document.getElementById(\"TheIdOfElementWeWishToClick\");\n" +
" cb.dispatchEvent(evt);"
+ "%"));
早些时候我正在尝试
System.out.println(webEngine.executeScript("document.getElementById(\"TheIdOfElementWeWishToClick\").click();");
哪个不起作用,因为“.click()”不起作用。