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监测的资料请关注我们其它相关文章!

(0)

相关推荐

  • selenium+python自动化测试之使用webdriver操作浏览器的方法

    WebDriver简介 selenium从2.0开始集成了webdriver的API,提供了更简单,更简洁的编程接口.selenium webdriver的目标是提供一个设计良好的面向对象的API,提供了更好的支持进行web-app测试.从这篇博客开始,将学习使用如何使用python调用webdriver框架对浏览器进行一系列的操作 打开浏览器 在selenium+python自动化测试(一)–环境搭建中,运行了一个测试脚本,脚本内容如下: from selenium import webdri

  • selenium跳过webdriver检测并模拟登录淘宝

    简介 模拟登录淘宝已经不是一件新鲜的事情了,过去我曾经使用get/post方式进行爬虫,同时也加入IP代理池进行跳过检验,但随着大型网站的升级,采取该策略比较难实现了.因为你使用get/post方式进行爬取数据,会提示需要登录,而登录又是一大难题,需要滑动验证码验证.当你想使用IP代理池进行跳过检验时,发现登录时需要手机短信验证码验证,由此可以知道旧的全自动爬取数据对于大型网站比较困难了. selenium是一款优秀的WEB自动化测试工具,所以现在采用selenium进行半自动化爬取数据,支持模

  • Selenium+BeautifulSoup+json获取Script标签内的json数据

    Selenium爬虫遇到 数据是以 JSON 字符串的形式包裹在 Script 标签中, 假设Script标签下代码如下: <script id="DATA_INFO" type="application/json" > { "user": { "isLogin": true, "userInfo": { "id": 123456, "nickname":

  • c# Selenium爬取数据时防止webdriver封爬虫的方法

    背景 大家在使用Selenium + Chromedriver爬取网站信息的时候,以为这样就能做到不被网站的反爬虫机制发现.但是实际上很多参数和实际浏览器还是不一样的,只要网站进行判断处理,就能轻轻松松识别你是否使用了Selenium + Chromedriver模拟浏览器.其中 window.navigator.webdriver 就是很重要的一个. 问题窥探 正常浏览器打开是这样的 模拟器打开是这样的 ChromeOptions options = null; IWebDriver driv

  • Selenium执行Javascript脚本参数及返回值过程详解

    在Selenium中可以使用drvier.execute_script()来执行Javascript脚本,支持多行语句. 使用Javascript可以实现以下功能: 移除元素隐藏.禁用.只读等限制属性 为元素添加id或高亮样式 页面滚动 富文本框输入(HTML注入) 获取页面信息 使用Javascript参数 在使用Javascript语句时,还可以动态传入参数或元素对象,Javascript语句中使用占位符"argument[n]"来表示取第几个参数,如: js = "ar

  • Selenium执行JavaScript脚本的方法示例

    JavaScript是运行在客户端(浏览器)和服务器端的脚本语言,允许将静态网页转换为交互式网页.可以通过 Python Selenium WebDriver 执行 JavaScript 语句,在Web页面中进行js交互.那么js能做的事,Selenium应该大部分也能做.WebDriver是模拟终端用户的交互,所以就不能点击不可见的元素,有时可见元素也不能点击.在这些情况下,我们就可以通过WebDriver 执行JavaScript来点击或者执行页面元素.本文将介绍如何使用 WebDriver

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

    目录 selenium执行js 优点:直接调用浏览器的环境 障碍:绕过selenium监测 原理: # 执行js代码 bro.execute_script('js代码') 常见的selenium监测手段 正常登录 window.navigator.webdriver == undefined 自动化的 window.navigator.webdriver == true 除此之外,还有一些其它的标志性字符串(不同的浏览器可能会有所不同),常见的特征串如下所示: webdriver __drive

  • pyppeteer执行js绕过webdriver监测方法下

    目录 官方文档 启动pyppteer 切图 获取响应头,响应头状态,cookies 获取当前页面标题 获取页面html 第一种:获取整个页面html 第二种:只获取文本 注入JS,控制上下滚动 选择器 获取元素内部的文本.属性 get_html 模拟输入,鼠标点击 taobao登录 taobao修改检测浏览器 鼠标事件 键盘事件 使用ip代理ua 官方文档 https://miyakogi.github.io/pyppeteer/reference.html#mouse-class 启动pypp

  • pyppeteer执行js绕过webdriver监测方法上

    目录 Pyppeteer简介 下载 打开网页并截图 评估页面上的脚本 关键字参数的选项 选择器 基础用法 模拟输入 使用 tkinter 获取页面高度 宽度 爬取京东商城 爬取淘宝网 利用获取到的cookie 爬取搜索内容 针对iframe 的操作 Pyppeteer简介 Puppeteer 是 Google 基于 Node.js 开发的一个工具,有了它我们可以通过 JavaScript 来控制 Chrome 浏览器的一些操作,当然也可以用作网络爬虫上,其 API 极其完善,功能非常强大. 而

  • python自动化测试selenium执行js脚本实现示例

    WebDriver有2个方法执行Java Script脚本. (1)同步执行:execute_script (2)异步执行:execute_async_script from selenium import webdriver from time import sleep class TestScriptss(object): def setup(self): self.driver = webdriver.Chrome() self.driver.get(http://www.baidu.co

  • 用JS动态设置CSS样式常见方法小结(推荐)

    用JS来动态设置CSS样式,常见的有以下几种 1. 直接设置style的属性 某些情况用这个设置 !important值无效 如果属性有'-'号,就写成驼峰的形式(如textAlign) 如果想保留 - 号,就中括号的形式 element.style['text-align'] = '100px'; element.style.height = '100px'; 2. 直接设置属性(只能用于某些属性,相关样式会自动识别) element.setAttribute('height', 100);

  • python selenium执行所有测试用例并生成报告的方法

    直接上代码. # -*- coding: utf-8 -*- import time import os import os.path import re import unittest import HTMLTestRunner import shutil shutil.copyfile("setting.ini","../setting.ini") casepaths = [] def createsuite(casepath): testunit = unit

  • js实现操作cookie的常见方法总结【创建、读取、删除】

    本文实例讲述了js实现操作cookie的常见方法.分享给大家供大家参考,具体如下: js操作cookie,可以通过开源的插件实现,方便快捷,兼容性好,同样也可以自己写: 此文主要介绍两个常用的插件: Js.cookie.js 和 jQuery.cookie.js 0.Js.cookie.js js.cookie.js : 一个简单,轻量级的JavaScript API,用于处理cookie; 下载地址:https://github.com/js-cookie/js-cookie/releases

  • Python执行js字符串常见方法示例

    目录 方法 1--js2py 2--execjs 3--execjs 方法 执行大型js时有点慢 特殊编码的输入或输出参数会出现报错,解决方法: 可以把输入或输出的参数用base64编码一下.base64都是英文和数字,没有特殊字符了 1--js2py pip insatll js2py # 获取执行JS的环境 context = js2py.EvalJs() # 加载执行 context.execute('放JS字符代码') 2--execjs import execjs print(exec

  • python selenium 执行完毕关闭chromedriver进程示例

    因为使用多次以后发现进程中出现了很多chromedriver的残留,造成卡顿,所以决定优化一下. 这个问题困扰了楼主很久,百度谷歌查来查去都只有java,后面根据java和selenium结合看找出了python如何执行完把chromedriver进程关闭 Python的话控制chromedriver的开启和关闭的包是Service from selenium.webdriver.chrome.service import Service 创建的时候需要把chromedriver.exe的位置写

随机推荐