Skip to main content

SBCDP - Pure CDP (Chrome DevTools Protocol) Automation Framework

Project description

SBCDP - 纯CDP自动化框架

SBCDP - Pure CDP (Chrome DevTools Protocol) Automation Framework

项目来源 | Project Origin

SBCDP 是基于 SeleniumBase 项目重构而来的纯CDP自动化框架。提取了SeleniumBase中的CDP功能,并进行了完全重构,创建了一个清晰分离同步和异步操作的现代化自动化框架。

SBCDP is a pure CDP automation framework refactored from the SeleniumBase project. extracted the CDP functionality from SeleniumBase and completely refactored it to create a modern automation framework with clear separation of synchronous and asynchronous operations.

安装 | Installation

使用pip安装 | Install with pip

pip install sbcdp

开发版本安装 | Development Installation

pip install git+https://github.com/ConlinH/sbcdp

git clone https://github.com/ConlinH/sbcdp.git
cd sbcdp
pip install -e .

快速开始 | Quick Start

异步接口 | Asynchronous Interface

import asyncio
from sbcdp import AsyncChrome as Chrome

async def main():
    async with Chrome() as chrome:
        await chrome.get("https://httpbin.org/forms/post")
        await chrome.type('input[name="custname"]', "sbcdp 用户")
        await chrome.type('input[name="custtel"]', "123-456-7890")
        await chrome.type('input[name="custemail"]', "test@cdp-base.com")
        await chrome.type('textarea[name="comments"]', "这是使用sbcdp框架的测试")

        # 选择单选按钮
        await chrome.click('input[value="large"]')
        # 等待元素
        element = await chrome.find_element("button")
        await element.click()
        await chrome.sleep(2)

if __name__ == '__main__':
    asyncio.new_event_loop().run_until_complete(main())
    # asyncio.run(main())

同步接口 | Synchronous Interface

from sbcdp import SyncChrome as Chrome

with Chrome() as chrome:
    chrome.get("https://httpbin.org/forms/post")
    chrome.type('input[name="custname"]', "sbcdp 用户")
    chrome.type('input[name="custtel"]', "123-456-7890")
    chrome.type('input[name="custemail"]', "test@cdp-base.com")
    chrome.type('textarea[name="comments"]', "这是使用sbcdp框架的测试")

    # 选择单选按钮
    chrome.click('input[value="large"]')
    # 等待元素
    element = chrome.find_element("button")
    element.click()
    chrome.sleep(2)

5s盾 | cloudflare

import asyncio
from contextlib import suppress

from sbcdp import AsyncChrome as Chrome


async def main():
    # url = "https://fractal-testnet.unisat.io/explorer"
    url = "https://steamdb.info/"
    # url = "https://cn.airbusan.com/content/individual"
    # url = "https://pastebin.com/login"
    # url = "https://simple.ripley.com.pe/"
    # url = "https://www.e-food.gr/"
    async with Chrome() as chrome:
        await chrome.get(url)
        with suppress(Exception):
            await chrome.verify_cf("确认您是真人")
        await chrome.sleep(4)
        assert 'cf_clearance' in {c.name: c.value for c in await chrome.get_all_cookies()}
        print({c.name: c.value for c in await chrome.get_all_cookies()})


if __name__ == "__main__":
    asyncio.new_event_loop().run_until_complete(main())
    # asyncio.run(main())

拦截网络请求 | Intercept network requests

import asyncio
from contextlib import suppress

from sbcdp import AsyncChrome, NetHttp


async def main():
    async def cb1(data: NetHttp):
        print("monitor: ", data)

    async def cb2(data: NetHttp):
        print("intercept: ", data)
        # 拦截所有的图片请求
        if data.resource_type == 'Image':
            return True

    async with AsyncChrome() as sb:
        sb.http_monitor(monitor_cb=cb1, intercept_cb=cb2, delay_response_body=True)
        await sb.open("https://www.baidu.com")
        await sb.sleep(3)


if __name__ == "__main__":
    asyncio.new_event_loop().run_until_complete(main())
    # asyncio.run(main())

监听Websocket | Intercept Websocket

import asyncio
from sbcdp import AsyncChrome, NetWebsocket


async def ws_cb(msg: str, type_: str, ws: NetWebsocket):
    print(f"{type_}: {msg} ws: {ws}")


async def main():
    async with AsyncChrome() as sb:
        sb.ws_monitor(ws_cb)
        url = "https://toolin.cn/ws"
        await sb.open(url)
        ele = await sb.find_element_by_text('连接Websocket')
        await ele.click()
        await sb.sleep(.5)
        await sb.send_keys("input[placeholder='输入消息']", 'test msg')
        await sb.sleep(.1)
        ele = await sb.find_element_by_text('发 送')
        await ele.click()
        await sb.sleep(1)


if __name__ == '__main__':
    asyncio.new_event_loop().run_until_complete(main())
    # asyncio.run(main())

核心方法 | Core Methods

基础操作 | Basic Operations

  • get(url) - 导航到URL | Navigate to URL
  • click(selector) - 点击元素 | Click element
  • type(selector, text) - 输入文本 | Type text
  • get_text(selector) - 获取文本 | Get text
  • get_attribute(selector, attr) - 获取属性 | Get attribute
  • shadow_root_query_selector(selector) - 查询shadow dom | select shadow dom

增强交互 | Enhanced Interaction

  • mouse_click(selector) - 鼠标点击 | Mouse click
  • press_keys(selector, text) - 按键输入 | Press keys
  • focus(selector) - 聚焦元素 | Focus element
  • scroll_to_element(selector) - 滚动到元素 | Scroll to element

视觉效果 | Visual Effects

  • flash(selector) - 闪烁元素 | Flash element
  • highlight(selector) - 高亮元素 | Highlight element
  • highlight_overlay(selector) - 高亮覆盖 | Highlight overlay

表单操作 | Form Operations

  • select_option_by_text(selector, text) - 选择选项 | Select option
  • set_value(selector, value) - 设置值 | Set value
  • set_text(selector, text) - 设置文本 | Set text

截图功能 | Screenshot Functions

  • save_screenshot(filename) - 保存页面截图 | Save page screenshot
  • save_element_screenshot(selector, filename) - 保存元素截图 | Save element screenshot

架构设计 | Architecture Design

sbcdp/
├── core/           # 核心模块 | Core Modules
│   ├── chrome.py   # Chrome类 | Chrome Class
│   └── methods.py  # 方法实现 | Method Implementation
│   ...
├── driver/         # 驱动模块 | Driver Modules
├── config/         # 配置模块 | Configuration Modules
└── fixtures/       # 工具模块 | Utility Modules

测试 | Testing

运行测试 | Run Tests

# 运行所有测试 | Run all tests
pytest

# 运行同步测试 | Run sync tests
pytest tests/test_sync_chrome.py

# 运行异步测试 | Run async tests
pytest tests/test_async_chrome.py

# 带覆盖率测试 | Run with coverage
pytest --cov=sbcdp

许可证 | License

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

This project is licensed under the MIT License - see the LICENSE file for details.

致谢 | Acknowledgments

  • 感谢 SeleniumBase 项目提供的基础代码

  • 感谢所有贡献者的努力和支持

  • Thanks to the SeleniumBase project for providing the foundation code

  • Thanks to all contributors for their efforts and support

联系方式 | Contact


SBCDP - 让自动化更简单、更快速、更可靠!

SBCDP - Making automation simpler, faster, and more reliable!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sbcdp-1.3.1.tar.gz (101.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sbcdp-1.3.1-py3-none-any.whl (102.2 kB view details)

Uploaded Python 3

File details

Details for the file sbcdp-1.3.1.tar.gz.

File metadata

  • Download URL: sbcdp-1.3.1.tar.gz
  • Upload date:
  • Size: 101.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.10

File hashes

Hashes for sbcdp-1.3.1.tar.gz
Algorithm Hash digest
SHA256 043174ed5bdd6040bc607da998ba3142ec068bf82be83114502cf896c4af1210
MD5 e166e096c8a77d054405d0bc55d6294a
BLAKE2b-256 e2923211881fda4ae568f553a9809b1981b24ce1184b00cec393a2657b109443

See more details on using hashes here.

File details

Details for the file sbcdp-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: sbcdp-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 102.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.10

File hashes

Hashes for sbcdp-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1c11ff3b34a8ff7c247dc29a840d6ddbc85ada350597e396b13204278140def5
MD5 897cfe22087464fcab649088f6d5919f
BLAKE2b-256 6d3ddfe22e8eb4ba0087598572af38e698fbe07ad84851877371fc1c4b80d5a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page