Skip to main content
Version: 24.38.0

Locator 类

🌐 Locator class

定位器描述了一种定位对象并对其执行操作的策略。如果由于对象尚未准备好执行操作而导致操作失败,则整个操作会被重试。各种成功操作的先决条件会被自动检查。

🌐 Locators describe a strategy of locating objects and performing an action on them. If the action fails because the object is not ready for the action, the whole operation is retried. Various preconditions for a successful action are checked automatically.

详情请参见 https://pptr.nodejs.cn/guides/page-interactions#locators

🌐 See https://pptr.nodejs.cn/guides/page-interactions#locators for details.

语法

🌐 Signature

export declare abstract class Locator<T> extends EventEmitter<LocatorEvents>

Extends: EventEmitter<LocatorEvents>

属性

🌐 Properties

属性

🌐 Property

修饰符

🌐 Modifiers

类型

🌐 Type

描述

🌐 Description

_

optional

T

用于名义上键入 Locator

🌐 Used for nominally typing Locator.

timeout

readonly

number

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

click(this, options)

单击定位到的元素。

🌐 Clicks the located element.

clone()

克隆定位器。

🌐 Clones the locator.

fill(this, value, options)

使用提供的值填写由定位器标识的输入。输入的类型在运行时确定,并根据类型选择适当的填写方法。支持 contenteditable、select、textarea 和 input 元素。

🌐 Fills out the input identified by the locator using the provided value. The type of the input is determined at runtime and the appropriate fill-out method is chosen based on the type. contenteditable, select, textarea and input elements are supported.

filter(predicate)

创建根据定位值进行评估的期望。

🌐 Creates an expectation that is evaluated against located values.

如果期望不匹配,则定位器将重试。

🌐 If the expectations do not match, then the locator will retry.

hover(this, options)

悬停在定位元素上。

🌐 Hovers over the located element.

map(mapper)

使用提供的映射器映射定位器。

🌐 Maps the locator using the provided mapper.

race(locators)

static

在多个定位器之间创建竞争,这些定位器试图并行定位元素,但确保只有一个元素接收操作。

🌐 Creates a race between multiple locators trying to locate elements in parallel but ensures that only a single element receives the action.

scroll(this, options)

滚动定位到的元素。

🌐 Scrolls the located element.

setEnsureElementIsInTheViewport(this, value)

通过克隆当前定位器并指定定位器是否应将元素滚动到视口中(如果元素尚未在视口中)来创建一个新的定位器实例。

🌐 Creates a new locator instance by cloning the current locator and specifying whether the locator should scroll the element into viewport if it is not in the viewport already.

setTimeout(timeout)

通过克隆当前定位器并设置定位器操作的总超时来创建一个新的定位器实例。

🌐 Creates a new locator instance by cloning the current locator and setting the total timeout for the locator actions.

传递 0 来禁用超时。

🌐 Pass 0 to disable timeout.

setVisibility(this, visibility)

通过克隆当前定位器并将可见性属性更改为指定值来创建新的定位器实例。

🌐 Creates a new locator instance by cloning the current locator with the visibility property changed to the specified value.

setWaitForEnabled(this, value)

通过克隆当前定位器并指定在执行操作前是否等待输入元素可用来创建一个新的定位器实例。适用于 clickfill 操作。

🌐 Creates a new locator instance by cloning the current locator and specifying whether to wait for input elements to become enabled before the action. Applicable to click and fill actions.

setWaitForStableBoundingBox(this, value)

通过克隆当前定位器并指定定位器是否必须等待元素的边界框在两个连续的动画帧之间相同来创建一个新的定位器实例。

🌐 Creates a new locator instance by cloning the current locator and specifying whether the locator has to wait for the element's bounding box to be same between two consecutive animation frames.

wait(options)

等待定位器从页面获取序列化值。

🌐 Waits for the locator to get the serialized value from the page.

请注意,这要求该值是 JSON 可序列化的。

🌐 Note this requires the value to be JSON-serializable.

waitHandle(options)

等待定位器从页面获取句柄。

🌐 Waits for the locator to get a handle from the page.