• 【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
  • 相关阅读:
    Linux、Unix、WindowsC语言理解查看字节序排序
    案例分享-智慧景区智能管控系统
    二叉搜索树
    飞腾全国产化加固计算机
    stm32f334定时器配置详细解释
    【C++语言】继承
    FLStudio21无需切换中文语言fl下载中文免费版
    Spring Security 从入门到精通之获取认证用户信息(四)
    Java设计模式-访问者模式
    linux搭建邮件服务器 - postifx + SSL + 465端口配置
  • 原文地址:https://blog.csdn.net/qq_35240081/article/details/136666348