Skip to content

Commit 66491e0

Browse files
authored
Add WaitForFunctionAsync doc (#1504)
1 parent 44bc751 commit 66491e0

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# How to wait for a javascript expression to be true
2+
_Contributors: [Darío Kondratiuk](https://www.hardkoded.com)_
3+
4+
## Problem
5+
6+
`WaitForSelectorAsync` is not enough, you want to wait for a more complex javascript expression to be truthly.
7+
8+
## Solution
9+
10+
Use [Page.WaitForExpressionAsync](https://www.puppeteersharp.com/api/PuppeteerSharp.Page.html#PuppeteerSharp_Page_WaitForExpressionAsync_System_String_PuppeteerSharp_WaitForFunctionOptions_) or [Page.WaitForFunctionAsync](https://www.puppeteersharp.com/api/PuppeteerSharp.Page.html#PuppeteerSharp_Page_WaitForFunctionAsync_System_String_PuppeteerSharp_WaitForFunctionOptions_System_Object___) to delay execution until the result of a javascription expression is truthly.
11+
12+
If it's a simple expression you can use `WaitForFunctionAsync`:
13+
14+
```cs
15+
using (var browser = await Puppeteer.LaunchAsync(options))
16+
using (var page = await browser.NewPageAsync())
17+
{
18+
await page.GoToAsync("https://www.somepage.com");
19+
await Page.WaitForExpressionAsync("document.queryselector('#status_info').innerText.match('^Showing ([1-9][0-9]*?) to ([1-9][0-9]*?)') of ([1-9][0-9]*?) entries') != null");
20+
}
21+
```
22+
23+
If the evaluation is more complex, you could wrap it inside a function and use `WaitForFunctionAsync`:
24+
25+
```cs
26+
var waitTask = Page.WaitForFunctionAsync(@"() =>
27+
{
28+
return document.queryselector('#status_info').innerText.match('^Showing ([1-9][0-9]*?) to ([1-9][0-9]*?)') of ([1-9][0-9]*?) entries') != null;
29+
}");
30+
```

docfx_project/examples/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
href: ReuseChrome.md
1313
- name: How to test a Chrome Extension
1414
href: ChromeExtension.md
15+
- name: How to wait for a javascript expression to be true
16+
- href: Page.WaitForFunctionAsync.md
1517
- name: Advanced
1618
items:
1719
- name: How to log CDP communication

0 commit comments

Comments
 (0)