使用.click()时页面元素没有响应怎么办?如果是React开发的页面,大概率是没有设置事件监听。首先看看有没有绑定事件监听器:F12->CTRL+SHIFT+C查看元素->事件监听器选项卡有绑定事件监听器的元素:没有绑定事件监听器的元素:无绑定事件监听器元素的解决方法:constele=document.querySelector("#nameZh");constprop=Object.keys(zh).find(p=>p.startsWith('__reactEventHandlers'));console.log(ele[prop]);上面最关键的代码就是第二行获取变量prop,通过遍历key值获取react元素绑定的方法。然后触发这些事件。letmousedown=newEvent("mousedown",{"bubbles":true,"cancel":true,"composed":true});letkeyup=newEvent('keyup',{"bubbles":true,"cancel":true,"composed":true});varclick=newEvent('click',{"bubbles":true,"cancel":true,"composed":true});letele=document.querySelector("selector")ele.dispatchEvent(mousedown)ele.dispatchEvent(keyup)ele.dispatchEvent(click)如何在不使用异步操作的情况下一步步执行代码?封装所有函数并以Promise形式返回示例:```//填充指定的输入框//@param{string}selector//@param{string}contentconstfillInput=(selector,content)=>{returnnewPromise(resolve=>{setTimeout(()=>{varmousedown=newEvent("mousedown",{"bubbles":true,"cancel":true,"composed":true});varkeyup=newEvent('keyup',{"气泡":true,"取消":true,"组合":true});让ele=document.querySelector(selector)ele.focus();document.execCommand("inserttext",false,content)ele.dispatchEvent(mousedown);ele.dispatchEvent(keyup);//console.log(选择器)resolve();},300);})}调用:fillInput(zhTitSelector,zh_title).then(()=>fillInput(zhContSelector,zh_content)).then(()=>fillInput(enTitSelector,en_title)).then(()=>fillInput(enContSelector,en_content))```更改了输入值,但未通过验证,点击保存值将被清除。有2种可能的解决方案,事件监听器按顺序执行,例如:focusfocus->input输入内容->changecontentchange->blurunfocus如果事件监听器有这些,按照上面一一触发方法,调用document.execCommand("inserttext",false,"username")API,这个api可以模拟人的操作,但是2023年2月20日已经从相关的web标准中移除了,对我的项目有用。参考油猴脚本开发指南一文通过js脚本更改input元素的值设置值时无法触发change事件的解决方法
