Javascript模拟真实点击代码分享
在做爬虫时经常会涉及到模拟点击、模拟输入等场景,当时很多情况下elment.click() 并不会触发事件 今天给大家分享一个模拟真实点击的效果
直接见代码
const input = document.querySelectorAll(".xxx")[0];
const phone = 'xxxxxxxxxxxx';
// 设置值
input.value = phone;
// 触发各种事件
input.dispatchEvent(new Event('focus', { bubbles: true }));
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
input.dispatchEvent(new Event('blur', { bubbles: true }));
发表评论