ha4t.cdp.cdp module

CDP 类用于与 Chrome DevTools Protocol (CDP) 进行通信。它通过 WebSocket 连接到浏览器的调试端口,并发送 CDP 命令以控制浏览器。

该模块提供了与浏览器进行交互的功能,包括发送命令、接收响应、获取页面元素等。

使用示例:

  1. 创建 WS_CDP 实例: `python cdp = CDP("ws://localhost:9222") `

  2. 输入page标题匹配连接: `python page = cdp.get_page("homepage") `

  3. 使用 Page 类与浏览器窗口交互: `python page.click(("css selector","#element_id")) `

  4. 获取元素并进行操作: `python element = page.get_element(("css selector", "#element_id")) Element.click() `

  5. 截图: `python img = page.screenshot("screenshot.png") `

class Page(ws, wait_page_ready_timeout=30)[源代码]

基类:object

restart()[源代码]

重新启动页面。

send(method, params=None, timeout=3)[源代码]

发送命令到浏览器并获取结果。

参数:
  • method -- CDP 方法名

  • params -- 方法参数,默认为 None

  • timeout -- 超时时间,默认为 3 秒

返回:

响应结果

抛出:

ValueError -- 如果超时

Example:
>>> result = Page().send("Page.navigate", {"url": "https://www.example.com"})
execute_script(script) Any[源代码]

执行 JavaScript 脚本并返回结果。

参数:

script -- 要执行的 JavaScript 脚本

返回:

执行结果

Example:
>>> result = Page().execute_script("document.title;")
get_element(locator: tuple, timeout=None) Element[源代码]

获取页面元素并返回 Element 实例。 :param locator: 元素定位器,格式为元组,例如 ("css selector", "#element_id") :param timeout: 超时时间,默认为配置中的 FIND_TIMEOUT :return: Element 实例 :raises ValueError: 如果元素定位失败 :Example:

>>> element = Page().get_element(("css selector", "#element_id"))
get_title()[源代码]

获取当前页面标题。

返回:

页面标题

Example:
>>> title = Page().get_title()
exist(locator: tuple)[源代码]

判断元素是否存在。

参数:

locator -- 元素定位器

返回:

如果元素存在返回 True,否则返回 False

Example:
>>> exists = Page().exist(("css selector", "#element_id"))
wait(locator: tuple, timeout=None)[源代码]

等待元素出现。

参数:
  • locator -- 元素定位器

  • timeout -- 超时时间,默认为配置中的 FIND_TIMEOUT

抛出:

ValueError -- 如果超时

Example:
>>> Page().wait(("css selector", "#element_id"))
screenshot(path=None) <MagicMock name='mock.Image.Image' id='140069202039232'>[源代码]

截图并返回图像对象。

参数:

path -- 可选,保存截图的路径

返回:

PIL.Image.Image 对象

Example:
>>> img = Page().screenshot("screenshot.png")
click(locator: tuple, timeout=None)[源代码]

点击元素。

参数:
  • locator -- 元素定位器

  • timeout -- 超时时间,默认为配置中的 FIND_TIMEOUT

Example:
>>> Page().click(("css selector", "#element_id"))
static command(method, params=None)[源代码]

构建命令元组。

参数:
  • method -- CDP 方法名

  • params -- 方法参数,默认为 None

返回:

(method, params) 元组

Example:
>>> cmd = Page().command("Page.navigate", {"url": "https://www.example.com"})
close()[源代码]

关闭页面并等待工作线程结束。 :Example:

>>> Page().close()
class CDP(url='http://localhost:9222')[源代码]

基类:object

get_page(ws_title: str | list[str] = None, timeout=30) Page[源代码]

获取页面实例。

参数:
  • ws_title -- 页面标题,可以是字符串或字符串列表

  • timeout -- 超时时间,默认为 30 秒

返回:

Page 实例

抛出:

ValueError -- 如果获取页面超时

Example:
>>> page = CDP().get_page("页面标题")
get_page_list() list[源代码]

获取当前打开的页面列表。

返回:

页面列表

Example:
>>> pages = CDP.get_page_list()
class Element(page: Page, element_id: str)[源代码]

基类:object

click()[源代码]

点击元素。

Example:
>>> Element().click()
get_text()[源代码]

获取元素的文本内容。

返回:

元素的文本内容

Example:
>>> text = Element().get_text()
get_value()[源代码]

获取元素的值。

返回:

元素的值

Example:
>>> value = Element().get_value()
set_value(value)[源代码]

设置元素的值。

参数:

value -- 要设置的值

Example:
>>> Element().set_value("value")
get_attribute(attribute)[源代码]

获取元素的属性。

参数:

attribute -- 属性名

返回:

属性值

Example:
>>> attribute_value = Element().get_attribute("attribute_name")
set_attribute(attribute, value)[源代码]

设置元素的属性。

参数:
  • attribute -- 属性名

  • value -- 属性值

Example:
>>> Element().set_attribute("attribute_name", "attribute_value")
scroll_to()[源代码]

滚动到元素。

Example:
>>> Element().scroll_to()
get_location()[源代码]

获取元素在页面上的位置。

返回:

包含 x, y, width, height 的字典

Example:
>>> location = Element().get_location()