解决Puppeteersharp 被检测到的方法, 顺带学习了js如何实现 模拟点击拖动事件
var launchOptions = new LaunchOptions { Headless = false, DefaultViewport = null, IgnoreHTTPSErrors = true, ExecutablePath = path + "\\.local-chromium\\chrome-win\\chrome.exe", Args = new string[] { "--no-sandbox", "--start-maximized", "--window-size=800,800", "--disable-setuid-sandbox", "--disable-accelerated-2d-canvas", "--no-zygote", "--renderer-process-limit=1", "--no-first-run", "--ignore-certificate-errors", "--ignore-certificate-errors-spki-list", "--disable-dev-shm-usage", "--disable-infobars", "--disable-extensions", "--disable-web-security", "--disable-blink-features=AutomationControlled", // 重点就是这行代码, 让自动化不可见 //"--user-agent=Mozilla/5.0" //"--user-agent=Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/58.0.3029.110 Safari/537.36" "--user-agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36\"" // 设置头, 不设置也可以 // } };
"--disable-blink-features=AutomationControlled",
------------- ------------- -------------------------- -------------
以下代码为js拖动
event = document.createEvent('MouseEvents'); event.initEvent('mousedown', true, false); document.querySelector("#nc_1_n1z").dispatchEvent(event); event = document.createEvent('MouseEvents'); event.initEvent('mousemove', true, false); Object.defineProperty(event,'clientX',{get(){return 260;}}) document.querySelector("#nc_1_n1z").dispatchEvent(event);
实现模拟拖动
模拟鼠标点击 // 找到要操作的 div 元素 var divElement = document.querySelector('.nc-lang-cnt'); // 创建一个鼠标点击事件 var mouseClickEvent = new MouseEvent('mousedown', { bubbles: true, cancelable: true, view: window }); // 创建一个触摸结束事件 var touchEndEvent = new TouchEvent('touchend', { bubbles: true, cancelable: true, view: window }); // 触发鼠标点击事件 divElement.dispatchEvent(mouseClickEvent); // 触发触摸结束事件 divElement.dispatchEvent(touchEndEvent);
大道三千,条条皆可证道。道心不可失。