Documentation has been updated: see help center and changelog in one place.

指令列表

通用参数

下面定义的所有指令具有一组一致的参数。参数如下。

type

  • 类型: Enum["click", "input", "scroll", "scroll_to_bottom", "wait", "wait_for_element", "fetch_resource"]

  • 说明: 浏览器指令类型。

timeout_s

  • 类型: int

  • 说明: 如果在规定时间内未完成,动作被跳过前的等待时长。

  • 限制: 0 < timeout_s <= 60

  • 默认值: 5

wait_time_s

  • 类型: int

  • 说明: 在执行下一个动作前等待的时长。

  • 限制: 0 < wait_time_s <= 60

  • 默认值: 0

on_error

  • 类型: Enum["error", "skip"]

  • 说明: 指示当此指令失败时对后续指令的处理:

    • "error": 停止浏览器指令的执行。

    • "skip": 继续执行下一条指令。

  • 默认值: "error"

包含通用参数的示例

{
    "type": "wait_for_element",
    "selector": {
        "type": "text",
        "value": "加载更多项目"
    },
    "timeout_s": 5,
    "wait_time_s": 2,
    "on_error": "skip"

}

指令

click

  • 说明: 点击某个元素并等待设定的秒数。

  • 参数:

    • type: str = "click"

    • selector: dict

      • type: Enum["xpath", "css", "text"]

      • value: str

  • 示例:

{
    "type": "click",
    "selector": {
        "type": "xpath",
        "value": "//button"
    }
}

input

  • 说明: 在选定的元素中输入文本。

  • 参数:

    • type: str = "input"

    • selector: dict

      • type: Enum["xpath", "css", "text"]

      • value: str

    • value: str

    • 示例:

{
    "type": "input",
    "selector": {
        "type": "xpath",
        "value": "//input"
    },
    "value": "披萨盒"
}

scroll

  • 说明: 滚动指定像素数量。

  • 参数:

    • type: str = "scroll"

    • x: int

    • y: int

  • 示例:

{
    "type": "scroll",
    "x": 0,
    "y": 100
}

scroll_to_bottom

  • 说明: 在指定秒数内向页面底部滚动。

  • 参数:

    • type: str = "scroll_to_bottom"

  • 示例:

{
    "type": "scroll_to_bottom",
    "timeout_s": 10
}

wait

  • 说明: 等待指定的秒数。

  • 参数:

    • type: str = "wait"

  • 示例:

{
    "type": "wait",
    "wait_time_s": 2
}

wait_for_element

  • 说明: 等待元素在指定秒数内加载。

  • 参数:

    • type: str = "wait_for_element"

    • selector: dict

      • type: Enum["xpath", "css", "text"]

      • value: str

    • 示例:

{
    "type": "wait_for_element",
    "selector": {
        "type": "text",
        "value": "加载更多项目"
    },
    "timeout_s": 5
}

fetch_resource

  • 说明: 获取与设定模式匹配的第一个 Fetch/XHR 资源。

  • 参数:

    • type: str = "fetch_resource"

    • filter: str(RegEx expression)

    • on_error: Enum["error", "skip"]

  • 示例:

{
    "type": "fetch_resource",
    "filter": "/graphql/item/"
}

指令验证

任何与指令格式不一致的情况将导致一个 400 状态码和相应的错误信息。

例如,以下载荷:

{
    "source": "universal",
    "url": "https://www.example.com/",
    "render": "html",
    "browser_instructions": [
        {
            "type": "unsupported-wait",
            "wait_time_s": 5
        }
    ]
}

将导致:

{    
    "errors": {
        "message": "Unsupported action type `unsupported-wait`, choose from 'click,fetch_resource,input,scroll,scroll_to_bottom,wait,wait_for_element'"
    }
}

最后更新于

这有帮助吗?