autogen_ext.agents.web_surfer.playwright_controller#

class PlaywrightController(downloads_folder: str | None = None, animate_actions: bool = False, viewport_width: int = 1440, viewport_height: int = 900, _download_handler: Callable[[Download], None] | None = None, to_resize_viewport: bool = True)[source]#

基类: object

一个辅助类,允许 Playwright 与网页交互,执行点击、填写和滚动等操作。

参数:
  • downloads_folder (str | None) – 下载保存的文件夹。如果为 None,则不保存下载。

  • animate_actions (bool) – 是否动画化操作(创建模拟光标进行点击)。

  • viewport_width (int) – 视口的宽度。

  • viewport_height (int) – 视口的高度。

  • _download_handler (Optional[Callable[[Download], None]]) – 处理下载的函数。

  • to_resize_viewport (bool) – 是否调整视口大小。

async sleep(page: Page, duration: int | float) None[source]#

暂停执行指定时长。

参数:
  • page (Page) – Playwright 页面对象。

  • duration (Union[int, float]) – 暂停的时长(毫秒)。

async get_interactive_rects(page: Page) Dict[str, InteractiveRegion][source]#

从网页中检索交互区域。

参数:

page (Page) – Playwright 页面对象。

返回:

Dict[str, InteractiveRegion] – 交互区域的字典。

async get_visual_viewport(page: Page) VisualViewport[source]#

检索网页的可视视口。

参数:

page (Page) – Playwright 页面对象。

返回:

VisualViewport – 页面可视视口。

async get_focused_rect_id(page: Page) str | None[source]#

检索当前聚焦元素的 ID。

参数:

page (Page) – Playwright 页面对象。

返回:

str – 聚焦元素的 ID,如果没有控件聚焦则为 None。

async get_page_metadata(page: Page) Dict[str, Any][source]#

从网页中检索元数据。

参数:

page (Page) – Playwright 页面对象。

返回:

Dict[str, Any] – 页面元数据的字典。

async on_new_page(page: Page) None[source]#

处理新页面上的操作。

参数:

page (Page) – Playwright 页面对象。

async back(page: Page) None[source]#

导航回上一页。

参数:

page (Page) – Playwright 页面对象。

async visit_page(page: Page, url: str) Tuple[bool, bool][source]#

访问指定的 URL。

参数:
  • page (Page) – Playwright 页面对象。

  • url (str) – 要访问的 URL。

返回:

Tuple[bool, bool] – 一个元组,指示是否重置先前的元数据哈希和上次下载。

async page_down(page: Page) None[source]#

向下滚动页面,滚动距离为视口高度减去 50 像素。

参数:

page (Page) – Playwright 页面对象。

async page_up(page: Page) None[source]#

向上滚动页面,滚动距离为视口高度减去 50 像素。

参数:

page (Page) – Playwright 页面对象。

async gradual_cursor_animation(page: Page, start_x: float, start_y: float, end_x: float, end_y: float) None[source]#

逐渐动画化光标从起始坐标到结束坐标的移动。

参数:
  • page (Page) – Playwright 页面对象。

  • start_x (float) – 起始 x 坐标。

  • start_y (float) – 起始 y 坐标。

  • end_x (float) – 结束 x 坐标。

  • end_y (float) – 结束 y 坐标。

async add_cursor_box(page: Page, identifier: str) None[source]#

在给定标识符的元素周围添加一个红色光标框。

参数:
  • page (Page) – Playwright 页面对象。

  • identifier (str) – 元素标识符。

async remove_cursor_box(page: Page, identifier: str) None[source]#

移除给定标识符的元素周围的红色光标框。

参数:
  • page (Page) – Playwright 页面对象。

  • identifier (str) – 元素标识符。

async click_id(page: Page, identifier: str) Page | None[source]#

点击给定标识符的元素。

参数:
  • page (Page) – Playwright 页面对象。

  • identifier (str) – 元素标识符。

返回:

Page | None – 如果打开新页面,则为新页面,否则为 None。

async hover_id(page: Page, identifier: str) None[source]#

将鼠标悬停在给定标识符的元素上。

参数:
  • page (Page) – Playwright 页面对象。

  • identifier (str) – 元素标识符。

async fill_id(page: Page, identifier: str, value: str, press_enter: bool = True) None[source]#

用指定值填充给定标识符的元素。

参数:
  • page (Page) – Playwright 页面对象。

  • identifier (str) – 元素标识符。

  • value (str) – 要填充的值。

async scroll_id(page: Page, identifier: str, direction: str) None[source]#

沿指定方向滚动给定标识符的元素。

参数:
  • page (Page) – Playwright 页面对象。

  • identifier (str) – 元素标识符。

  • direction (str) – 滚动方向(“up”或“down”)。

async get_webpage_text(page: Page, n_lines: int = 50) str[source]#

检索网页的文本内容。

参数:
  • page (Page) – Playwright 页面对象。

  • n_lines (int) – 从页面内部文本返回的行数。

返回:

str – 页面的文本内容。

async get_visible_text(page: Page) str[source]#

检索浏览器视口的文本内容(大约)。

参数:

page (Page) – Playwright 页面对象。

返回:

str – 页面的文本内容。

async get_page_markdown(page: Page) str[source]#

检索网页的 Markdown 内容。目前尚未实现。

参数:

page (Page) – Playwright 页面对象。

返回:

str – 页面的 Markdown 内容。