最近学习用C#的webdriver写自动化网页程序,做到隐藏selenium特征时,看到大部分使用stealth.min.js的都是Python写的,这边记录一下C#中用stealth.min.js隐藏selenium特征的方法。   

System.IO.StreamReader sr = new System.IO.StreamReader($"{Environment.CurrentDirectory}\\stealth.min.js");

string line = sr.ReadToEnd();

Dictionary properties = new Dictionary();

properties.Add("source", line);

webDriver.ExecuteCdpCommand("Page.addScriptToEvaluateOnNewDocument", properties);

        原理与python中一样,就是在初始化chromedriver后调用cdp(即 Chrome DevTools Protocol)命令(需要selenium版本支持cdp),调用Page.addScriptToEvaluateOnNewDocument方法,让浏览器执行stealth.min.js文件中的代码。此时通过sannysoft之类的网站检测chromedriver为miss即为成功。如下图框出部分:

        cdp的API地址:Chrome DevTools Protocol - Page domain

        查询是否隐藏成功的网址贴上,可以对比:sannysoft.com

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: