selenium执行js并绕过webdriver监测常见方法

这篇文章主要为大家介绍了selenium执行js并绕过webdriver监测常见方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪

selenium执行js

优点:直接调用浏览器的环境
障碍:绕过selenium监测

原理:

# 执行js代码 bro.execute_script('js代码') 

常见的selenium监测手段

正常登录 window.navigator.webdriver == undefined
自动化的 window.navigator.webdriver == true

除此之外,还有一些其它的标志性字符串(不同的浏览器可能会有所不同),常见的特征串如下所示:

webdriver __driver_evaluate __webdriver_evaluate __selenium_evaluate __fxdriver_evaluate __driver_unwrapped __webdriver_unwrapped __selenium_unwrapped __fxdriver_unwrapped _Selenium_IDE_Recorder _selenium calledSelenium _WEBDRIVER_ELEM_CACHE ChromeDriverw driver-evaluate webdriver-evaluate selenium-evaluate webdriverCommand webdriver-evaluate-response __webdriverFunc __webdriver_script_fn __$webdriverAsyncExecutor __lastWatirAlert __lastWatirConfirm __lastWatirPrompt $chrome_asyncScriptInfo $cdc_asdjflasutopfhvcZLmcfl_ 

了解了这个特点之后,就可以在浏览器客户端JS中通过检测这些特征串来判断当前是否使用了selenium,并将检测结果附加到后续请求之中,这样服务端就能识别并拦截后续的请求。

常用绕过selenium监测1

正常登录 window.navigator.webdriver == undefined
自动化的 window.navigator.webdriver == true

from selenium import webdriver options = webdriver.ChromeOptions() # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium options.add_experimental_option('excludeSwitches', ['enable-automation']) #停止加载图片 options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) browser = webdriver.Chrome(options=options) browser.get('https://www.taobao.com/') 

常用绕过selenium监测2

from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option('debuggerAddress','127.0.0.1:9222') browser=webdriver.Chrome(executable_path=r'C:\Users\TR\AppData\Local\Google\Chrome \Application\chromedriver.exe',chrome_options=chrome_options) browser.get('http://www.zhihu.com') 

终端输入如下指令:chrome.exe --remote-debugging-port=9222 --user-data-dir=“D:\cdsf”(需要谷歌驱动在系统环境变量下,然后再运行程序)

remote-debugging-port是你代码中指定的端口debuggerAddress;executable_path是你谷歌驱动位置;user-data-dir随便指定一个目录就行

常用绕过selenium监测3

1.使用chrome的远程调试模式结合selenium来遥控chrome进行抓取,这样不会携带指纹信息

步骤:

- 使用调试模式手工启动chrome,进入chrome的安装路径,例如chrome装在 C:\program\google\chrome.exe下
- 进入chrome安装路径
- 执行命令:
#注意端口不要被占用,防火墙要关闭,user-data-dir用来指明配置文件的路径
   chrome.exe --remote-debugging-port=9222 --user-data-dir="指向任意空文件夹"

2.启动完·之后新建python文件

运行代码:

import requests from selenium import webdriver chrome_options = "C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe" chrome_options  = webdriver.ChromeOptions() chrome_options.add_experimental_option('debuggerAddress','10.8.13.95:9222') browser = webdriver.Chrome(chrome_options=chrome_options) browser.get("https://www.zhihu.com/signup?next=%2F") # chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\moni" 

这样监测的就不是selenium模拟了

常用绕过selenium监测4

def selenium(js): option = webdriver.ChromeOptions() # option.add_argument('--headless') option.add_experimental_option('useAutomationExtension', False) option.add_experimental_option('excludeSwitches', ['enable-automation']) bro = webdriver.Chrome(executable_path='./chromedriver', options=option)  # 弹出浏览器,要给浏览器驱动的地址 # 打开页面优先执行的js,execute_cdp_cmd bro.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) bro.implicitly_wait(10) bro.get('https://www.toutiao.com/') time.sleep(5) print(bro.page_source)  # 获取页面返回的html代码 bro.execute_script(js) input()

以上就是selenium执行js并绕过webdriver监测常见方法的详细内容,更多关于selenium执行js绕过webdriver监测的资料请关注0133技术站其它相关文章!

以上就是selenium执行js并绕过webdriver监测常见方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python