例如一些富文本输入框、文件上传按钮 和自定义的文本框,按钮,
js代码运行后看似填写成功,但一提交数据就会消失,按钮点击无效,没有反应
使用浏览器的cdp协议中的
1.Input.dispatchKeyEvent(键盘按键)
2.Input.insertText(键盘文字输入)
3.Input.dispatchMouseEvent(鼠标点击)
4.Runtime.evaluate(运行js代码)
等方法模拟键盘和鼠标操作,绕过一些浏览器的限制
动作演示: https://getquicker.net/Sharedaction?code=06077b85-de15-4d8b-8067-08ddd99dc08d
1.Input.dispatchKeyEvent(键盘按键)
方法:就是模拟键盘按键,如:Tab键等
有的按键无效,不知道怎样设置,没有测试成功(F1-12等功能键)
type 事件类型 string: keyDown, keyUp, rawKeyDown, char
modifiers 修饰键类型 integer Alt=1, Ctrl=2 , Shift=8 (默认: 0) 作用是模拟按键Ctrl+A Ctrl+C Ctrl+V Shift+Tab等
1.Enter键
{
"key": "Enter",
"windowsVirtualKeyCode": 13,
"code": "Enter",
"text": "\r",
"unmodifiedText": "\r",
"type": "keyDown"
}
2.Delete键
{
"key": "Delete",
"windowsVirtualKeyCode": 46,
"code": "Delete",
"type": "keyDown"
}
在下方会提供一个按键表,需要的可以查询一下其表的数据格式,可能它要一定的格式才能触发按键
1.使用Input.dispatchKeyEvent(键盘按键Tab键)
注意:使用 type: "keyDown"时,是可以不用和type: "keyUp"连用的,只使用type: "keyDown"应该也是可以的。
浏览器插件的脚本代码(mv3)
var n={tabId: 824181119}
chrome.debugger.attach(n, "1.3")
按键Tab按下
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: "keyDown",key: "Tab"})
按键Tab弹起
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{type: "keyUp",key: "Tab"})
模拟全选Ctrl+A格式如下:
var n={tabId: 824182077}
chrome.debugger.attach(n, "1.3")
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: 'keyDown',windowsVirtualKeyCode: 17, code: 'ControlLeft', key: 'Control', modifiers: 0}) 可以不要
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: 'keyDown',windowsVirtualKeyCode: 65, key: 'A', code: 'KeyA',modifiers:2})
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: 'keyUp',windowsVirtualKeyCode: 65, key: 'A', code: 'KeyA',modifiers:2}) 可以不要
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{type: 'keyUp',windowsVirtualKeyCode: 17, code: 'ControlLeft', key: 'Control', modifiers: 0}) 可以不要
或者 需要试一试看看(选中)
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: 'keyDown',windowsVirtualKeyCode: 17, modifiers: 0})
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: 'keyDown',windowsVirtualKeyCode: 65,modifiers:2})
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{type: 'keyUp',windowsVirtualKeyCode: 17, modifiers: 0})
或者-复制( modifiers:2 这个参数的作用)
chrome.debugger.sendCommand(n,"Input.dispatchKeyEvent",{ type: 'keyDown',windowsVirtualKeyCode: 67,modifiers:2})
2.Input.insertText(键盘文字输入)
方法:先让文本框设置为有焦点状态-focus(),再使用Input.insertText(键盘文字输入)填写文本
1.js设置网页的输入框的焦点代码
document.querySelector("#text-editor").focus()
2.使用Input.insertText(键盘文字输入)
浏览器插件的脚本代码(mv3)
var n={tabId: 824181119}
chrome.debugger.attach(n, "1.3");
chrome.debugger.sendCommand( n,"Input.insertText",{text: "Hello, World!"});
3.或者使用这样的样式设置焦点和输入
chrome.debugger.sendCommand(n, "Runtime.evaluate", {expression: 'document.querySelectorAll(".text-editor")[1].focus()'});
chrome.debugger.sendCommand( n,"Input.insertText",{text: "Hello, World!"});
3.Input.dispatchMouseEvent(鼠标点击)
方法:使用js代码获取网页的按钮位置坐标,为点击传递数据坐标,再使用Input.dispatchMouseEvent(鼠标点击)
演示动作 :https://getquicker.net/Sharedaction?code=1defc102-436a-4ae3-806f-08ddd99dc08d
1.js获取网页的按钮位置坐标代码
var rect = document.querySelector("#button").getBoundingClientRect();
var x = rect.left + rect.width / 2;
var y = rect.top + rect.height / 2;
return [x,y]
2.使用Input.dispatchMouseEvent(鼠标点击按钮)
type 事件类型,如 "mousePressed"(按下)、"mouseReleased"(释放)、"mouseMoved"(移动)
button 按键类型,"left"(左键)、"right"(右键)、"middle"(中键)
浏览器插件的脚本代码(mv3)
var n={tabId: 824181119}
chrome.debugger.attach(n, "1.3");
鼠标左键按下(单击)
chrome.debugger.sendCommand(n, "Input.dispatchMouseEvent",{type: "mousePressed", x: x,y: y,button: "left",clickCount: 1});
鼠标左键弹起(单击)
chrome.debugger.sendCommand(n, "Input.dispatchMouseEvent",{type: "mouseReleased", x: x,y: y,button: "left",clickCount: 1});