diff --git a/docs/api/index.md b/docs/api/index.md index 0d2f5cf4..f4472822 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -29,7 +29,7 @@ sidebar_label: API | [JSCoverage](./puppeteer.jscoverage.md) | | | [JSHandle](./puppeteer.jshandle.md) |
Represents a reference to a JavaScript object. Instances can be created using [Page.evaluateHandle()](./puppeteer.page.evaluatehandle.md).
Handles prevent the referenced JavaScript object from being garbage-collected unless the handle is purposely [disposed](./puppeteer.jshandle.dispose.md). JSHandles are auto-disposed when their associated frame is navigated away or the parent context gets destroyed.
Handles can be used as arguments for any evaluation function such as [Page.$eval()](./puppeteer.page._eval.md), [Page.evaluate()](./puppeteer.page.evaluate.md), and [Page.evaluateHandle()](./puppeteer.page.evaluatehandle.md). They are resolved to their referenced object.
| | [Keyboard](./puppeteer.keyboard.md) | Keyboard provides an api for managing a virtual keyboard. The high level api is [Keyboard.type()](./puppeteer.keyboard.type.md), which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page. | -| [Locator](./puppeteer.locator.md) | Locators describe a strategy of locating elements and performing an action on them. If the action fails because the element is not ready for the action, the whole operation is retried. Various preconditions for a successful action are checked automatically. | +| [Locator](./puppeteer.locator.md) | 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. | | [Mouse](./puppeteer.mouse.md) | The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport. | | [Page](./puppeteer.page.md) |Page provides methods to interact with a single tab or [extension background page](https://developer.chrome.com/extensions/background_pages) in the browser.
:::note
One Browser instance might have multiple Page instances.
:::
| | [ProductLauncher](./puppeteer.productlauncher.md) | Describes a launcher - a class that is able to create and launch a browser instance. | diff --git a/docs/api/puppeteer.frame.locator.md b/docs/api/puppeteer.frame.locator.md index eb19c571..f2eb7fbe 100644 --- a/docs/api/puppeteer.frame.locator.md +++ b/docs/api/puppeteer.frame.locator.md @@ -4,7 +4,7 @@ sidebar_label: Frame.locator # Frame.locator() method -Creates a locator for the provided `selector`. See [Locator](./puppeteer.locator.md) for details and supported actions. +Creates a locator for the provided selector. See [Locator](./puppeteer.locator.md) for details and supported actions. #### Signature: diff --git a/docs/api/puppeteer.frame.locator_1.md b/docs/api/puppeteer.frame.locator_1.md new file mode 100644 index 00000000..a52a1c58 --- /dev/null +++ b/docs/api/puppeteer.frame.locator_1.md @@ -0,0 +1,29 @@ +--- +sidebar_label: Frame.locator_1 +--- + +# Frame.locator() method + +Creates a locator for the provided function. See [Locator](./puppeteer.locator.md) for details and supported actions. + +#### Signature: + +```typescript +class Frame { + locatorselector
. |
| [isDetached()](./puppeteer.frame.isdetached.md) | | Istrue
if the frame has been detached. Otherwise, false
. |
| [isOOPFrame()](./puppeteer.frame.isoopframe.md) | | Is true
if the frame is an out-of-process (OOP) frame. Otherwise, false
. |
-| [locator(selector)](./puppeteer.frame.locator.md) | | Creates a locator for the provided selector
. See [Locator](./puppeteer.locator.md) for details and supported actions. |
+| [locator(selector)](./puppeteer.frame.locator.md) | | Creates a locator for the provided selector. See [Locator](./puppeteer.locator.md) for details and supported actions. |
+| [locator(func)](./puppeteer.frame.locator_1.md) | | Creates a locator for the provided function. See [Locator](./puppeteer.locator.md) for details and supported actions. |
| [name()](./puppeteer.frame.name.md) | | The frame's name
attribute as specified in the tag. |
| [page()](./puppeteer.frame.page.md) | | The page associated with the frame. |
| [parentFrame()](./puppeteer.frame.parentframe.md) | | The parent frame, if any. Detached and main frames return null
. |
diff --git a/docs/api/puppeteer.locator.md b/docs/api/puppeteer.locator.md
index 86a6312b..9bfb137a 100644
--- a/docs/api/puppeteer.locator.md
+++ b/docs/api/puppeteer.locator.md
@@ -4,7 +4,7 @@ sidebar_label: Locator
# Locator class
-Locators describe a strategy of locating elements and performing an action on them. If the action fails because the element is not ready for the action, the whole operation is retried. Various preconditions for a successful action are checked automatically.
+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.
#### Signature:
diff --git a/docs/api/puppeteer.page.locator.md b/docs/api/puppeteer.page.locator.md
index ffb81c69..62ebcbfe 100644
--- a/docs/api/puppeteer.page.locator.md
+++ b/docs/api/puppeteer.page.locator.md
@@ -4,7 +4,7 @@ sidebar_label: Page.locator
# Page.locator() method
-Creates a locator for the provided `selector`. See [Locator](./puppeteer.locator.md) for details and supported actions.
+Creates a locator for the provided selector. See [Locator](./puppeteer.locator.md) for details and supported actions.
#### Signature:
diff --git a/docs/api/puppeteer.page.locator_1.md b/docs/api/puppeteer.page.locator_1.md
new file mode 100644
index 00000000..72c3f30f
--- /dev/null
+++ b/docs/api/puppeteer.page.locator_1.md
@@ -0,0 +1,29 @@
+---
+sidebar_label: Page.locator_1
+---
+
+# Page.locator() method
+
+Creates a locator for the provided function. See [Locator](./puppeteer.locator.md) for details and supported actions.
+
+#### Signature:
+
+```typescript
+class Page {
+ locatortrue
if drag events are being intercepted, false
otherwise. |
| [isJavaScriptEnabled()](./puppeteer.page.isjavascriptenabled.md) | | true
if the page has JavaScript enabled, false
otherwise. |
| [isServiceWorkerBypassed()](./puppeteer.page.isserviceworkerbypassed.md) | | true
if the service worker are being bypassed, false
otherwise. |
-| [locator(selector)](./puppeteer.page.locator.md) | | Creates a locator for the provided selector
. See [Locator](./puppeteer.locator.md) for details and supported actions. |
+| [locator(selector)](./puppeteer.page.locator.md) | | Creates a locator for the provided selector. See [Locator](./puppeteer.locator.md) for details and supported actions. |
+| [locator(func)](./puppeteer.page.locator_1.md) | | Creates a locator for the provided function. See [Locator](./puppeteer.locator.md) for details and supported actions. |
| [mainFrame()](./puppeteer.page.mainframe.md) | | The page's main frame. |
| [metrics()](./puppeteer.page.metrics.md) | | Object containing metrics as key/value pairs. |
| [off(eventName, handler)](./puppeteer.page.off.md) | | |
diff --git a/docs/guides/locators.md b/docs/guides/locators.md
index 0985a28d..9f707f43 100644
--- a/docs/guides/locators.md
+++ b/docs/guides/locators.md
@@ -1,9 +1,9 @@
# Locators
-Locators is a new experimental API that combines `waitForSelector` and element
-actions in a single unit. In combination with additional precondition checks
-this allows locators to retry failed actions automatically leading to less flaky
-automation scripts.
+Locators is a new, experimental API that combines the functionalities of
+waiting and actions. With additional precondition checks, it
+enables automatic retries for failed actions, resulting in more reliable and
+less flaky automation scripts.
:::note
@@ -12,7 +12,42 @@ in the Locators API.
:::
-## Clicking an element
+## Use cases
+
+### Waiting for an element
+
+```ts
+await page.locator('button').wait();
+```
+
+The following preconditions are automatically checked:
+
+- Waits for the element to become
+ [visible](https://pptr.dev/api/puppeteer.elementhandle.isvisible/) or hidden.
+
+### Waiting for a function
+
+```ts
+await page
+ .locator(() => {
+ let resolve!: (node: HTMLCanvasElement) => void;
+ const promise = new Promise(res => {
+ return (resolve = res);
+ });
+ const observer = new MutationObserver(records => {
+ for (const record of records) {
+ if (record.target instanceof HTMLCanvasElement) {
+ resolve(record.target);
+ }
+ }
+ });
+ observer.observe(document);
+ return promise;
+ })
+ .wait();
+```
+
+### Clicking an element
```ts
await page.locator('button').click();
@@ -27,7 +62,25 @@ The following preconditions are automatically checked:
- Waits for the element to have a stable bounding box over two consecutive
animation frames.
-## Filling out an input
+### Clicking an element matching a criteria
+
+```ts
+await page
+ .locator('button')
+ .filter(button => !button.disabled)
+ .click();
+```
+
+The following preconditions are automatically checked:
+
+- Ensures the element is in the viewport.
+- Waits for the element to become
+ [visible](https://pptr.dev/api/puppeteer.elementhandle.isvisible/) or hidden.
+- Waits for the element to become enabled.
+- Waits for the element to have a stable bounding box over two consecutive
+ animation frames.
+
+### Filling out an input
```ts
await page.locator('input').fill('value');
@@ -44,7 +97,16 @@ The following preconditions are automatically checked:
- Waits for the element to have a stable bounding box over two consecutive
animation frames.
-## Hover over an element
+### Retrieving an element property
+
+```ts
+const enabled = await page
+ .locator('button')
+ .map(button => !button.disabled)
+ .wait();
+```
+
+### Hover over an element
```ts
await page.locator('div').hover();
@@ -58,7 +120,7 @@ The following preconditions are automatically checked:
- Waits for the element to have a stable bounding box over two consecutive
animation frames.
-## Scroll an element
+### Scroll an element
```ts
await page.locator('div').scroll({
diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts
index c5e56872..ed105935 100644
--- a/packages/puppeteer-core/src/api/Frame.ts
+++ b/packages/puppeteer-core/src/api/Frame.ts
@@ -28,6 +28,7 @@ import {
import {LazyArg} from '../common/LazyArg.js';
import {PuppeteerLifeCycleEvent} from '../common/LifecycleWatcher.js';
import {
+ Awaitable,
EvaluateFunc,
EvaluateFuncWith,
HandleFor,
@@ -39,7 +40,7 @@ import {TaskManager} from '../common/WaitTask.js';
import {KeyboardTypeOptions} from './Input.js';
import {JSHandle} from './JSHandle.js';
-import {Locator, NodeLocator} from './locators/locators.js';
+import {Locator, FunctionLocator, NodeLocator} from './locators/locators.js';
/**
* @internal
@@ -417,7 +418,7 @@ export class Frame {
}
/**
- * Creates a locator for the provided `selector`. See {@link Locator} for
+ * Creates a locator for the provided selector. See {@link Locator} for
* details and supported actions.
*
* @remarks
@@ -426,10 +427,26 @@ export class Frame {
*/
locator