• 【python】自动化工具Selenium与playwright去除webdriver检测



    对这个世界如果你有太多的抱怨
    跌倒了就不敢继续往前走
    为什么人要这么的脆弱 堕落
    请你打开电视看看
    多少人为生命在努力勇敢的走下去
    我们是不是该知足
    珍惜一切 就算没有拥有
                         🎵 周杰伦《稻香》


    在这里插入图片描述

    # -*- coding:utf-8 -*-
    import time
    
    from  selenium import webdriver
    from selenium.webdriver.edge.service import Service
    
    option = webdriver.ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation']) # 去掉webdriver提示 + 顶层窗口
    option.add_argument("--disable-blink-features=AutomationControlled") # 屏蔽webdriver特征
    browser = webdriver.Chrome(options=option,executable_path="/Applications/Chromium.app/Contents/MacOS/Chromium")
    
    with open('webdriver_check.js', 'r', encoding='utf-8') as f:
        js = f.read()
        browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
            "source": js
        })
    
    time.sleep(10000)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    # -*- coding:utf-8 -*-
    import time
    
    from playwright.sync_api import sync_playwright
    
    
    def main():
        # 启动Playwright
        with sync_playwright() as p:
            # 选择浏览器引擎(chromium、firefox、webkit)
            browser = p.chromium.launch(headless=False)
    
            # 创建新的浏览器上下文
            context = browser.new_context()
    
            # 创建新的页面
            page = context.new_page()
            with open('webdriver_check.js', 'r', encoding='utf-8') as f:
                js = f.read()
                page.add_init_script(js)
    
            # 打开网页
            page.goto('https://www.baidu.com/')
    
            # 在页面中查找并填写表单
            time.sleep(500)
    
            # 关闭浏览器
            browser.close()
    
    
    if __name__ == "__main__":
        main()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    软考 - 网络工程师考试大纲
    Visual Studio 2022编写学校收费管理系统
    Java基础-IO流(字节流)
    小白备战大厂算法笔试(二)——数组、链表、列表
    C/C++内存管理
    网络文化经营许可证这样办,省时又便捷!
    什么是开关电源测试系统?如何用它进行测试?
    编译调试Net6源码
    8月更新 | Java on Azure Tooling
    【C语法学习】17 - fwrite()函数
  • 原文地址:https://blog.csdn.net/qq_35240081/article/details/136666348