docs: add python snippets for api classes (follow up) (#5018)

This commit is contained in:
Pavel Feldman 2021-01-14 11:09:44 -08:00 committed by GitHub
parent 3ed9f2d63e
commit 1648d23551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 37 additions and 52 deletions

View File

@ -93,7 +93,7 @@ def find_focused_node(node):
for child in (node.get("children") or []): for child in (node.get("children") or []):
found_node = find_focused_node(child) found_node = find_focused_node(child)
return found_node return found_node
return null return None
snapshot = await page.accessibility.snapshot() snapshot = await page.accessibility.snapshot()
node = find_focused_node(snapshot) node = find_focused_node(snapshot)
@ -108,7 +108,7 @@ def find_focused_node(node):
for child in (node.get("children") or []): for child in (node.get("children") or []):
found_node = find_focused_node(child) found_node = find_focused_node(child)
return found_node return found_node
return null return None
snapshot = page.accessibility.snapshot() snapshot = page.accessibility.snapshot()
node = find_focused_node(snapshot) node = find_focused_node(snapshot)

View File

@ -110,21 +110,21 @@ Creates a new browser context. It won't share cookies/cache with other browser c
``` ```
```python async ```python async
browser = await playwright.firefox.launch() # or "chromium" or "webkit". browser = await playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context. # create a new incognito browser context.
context = await browser.new_context() context = await browser.new_context()
# create a new page in a pristine context. # create a new page in a pristine context.
page = await context.new_page() page = await context.new_page()
await page.goto("https://example.com") await page.goto("https://example.com")
``` ```
```python sync ```python sync
browser = playwright.firefox.launch() # or "chromium" or "webkit". browser = playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context. # create a new incognito browser context.
context = browser.new_context() context = browser.new_context()
# create a new page in a pristine context. # create a new page in a pristine context.
page = context.new_page() page = context.new_page()
page.goto("https://example.com") page.goto("https://example.com")
``` ```
### option: Browser.newContext.-inline- = %%-shared-context-params-list-%% ### option: Browser.newContext.-inline- = %%-shared-context-params-list-%%

View File

@ -567,7 +567,8 @@ await browser.close();
```python async ```python async
context = await browser.new_context() context = await browser.new_context()
page = await context.new_page() page = await context.new_page()
await context.route(r"(\.png$)|(\.jpg$)", lambda page = await context.new_page() await context.route(r"(\.png$)|(\.jpg$)", lambda route => route.abort())
page = await context.new_page()
await page.goto("https://example.com") await page.goto("https://example.com")
await browser.close() await browser.close()
``` ```
@ -575,7 +576,8 @@ await browser.close()
```python sync ```python sync
context = browser.new_context() context = browser.new_context()
page = context.new_page() page = context.new_page()
context.route(r"(\.png$)|(\.jpg$)", lambda page = await context.new_page() context.route(r"(\.png$)|(\.jpg$)", lambda route => route.abort())
page = await context.new_page()
page = context.new_page() page = context.new_page()
page.goto("https://example.com") page.goto("https://example.com")
browser.close() browser.close()

View File

@ -48,7 +48,7 @@ from playwright.sync_api import sync_playwright
def handle_dialog(dialog): def handle_dialog(dialog):
print(dialog.message) print(dialog.message)
await dialog.dismiss() dialog.dismiss()
def run(playwright): def run(playwright):
chromium = playwright.chromium chromium = playwright.chromium

View File

@ -547,31 +547,31 @@ element, the method throws an error.
```js ```js
// single selection matching the value // single selection matching the value
handle.selectOption('select#colors', 'blue'); handle.selectOption('blue');
// single selection matching the label // single selection matching the label
handle.selectOption('select#colors', { label: 'Blue' }); handle.selectOption({ label: 'Blue' });
// multiple selection // multiple selection
handle.selectOption('select#colors', ['red', 'green', 'blue']); handle.selectOption(['red', 'green', 'blue']);
``` ```
```python async ```python async
# single selection matching the value # single selection matching the value
await handle.select_option("select#colors", "blue") await handle.select_option("blue")
# single selection matching the label # single selection matching the label
await handle.select_option("select#colors", label="blue") await handle.select_option(label="blue")
# multiple selection # multiple selection
await handle.select_option("select#colors", value=["red", "green", "blue"]) await handle.select_option(value=["red", "green", "blue"])
``` ```
```python sync ```python sync
# single selection matching the value # single selection matching the value
handle.select_option("select#colors", "blue") handle.select_option("blue")
# single selection matching both the label # single selection matching both the label
handle.select_option("select#colors", label="blue") handle.select_option(label="blue")
# multiple selection # multiple selection
handle.select_option("select#colors", value=["red", "green", "blue"]) handle.select_option(value=["red", "green", "blue"])
``` ```
```python sync ```python sync

View File

@ -1046,7 +1046,7 @@ async def run(playwright):
webkit = playwright.webkit webkit = playwright.webkit
browser = await webkit.launch() browser = await webkit.launch()
page = await browser.new_page() page = await browser.new_page()
watch_dog = page.main_frame.wait_for_function("() => window.innerWidth < 100") watch_dog = asyncio.create_task(page.main_frame.wait_for_function("() => window.innerWidth < 100")
await page.set_viewport_size({"width": 50, "height": 50}) await page.set_viewport_size({"width": 50, "height": 50})
await watch_dog await watch_dog
await browser.close() await browser.close()
@ -1057,22 +1057,6 @@ async def main():
asyncio.run(main()) asyncio.run(main())
``` ```
```python sync
from playwright.sync_api import sync_playwright
def run(playwright):
webkit = playwright.webkit
browser = await webkit.launch()
page = await browser.new_page()
watch_dog = page.main_frame.wait_for_function("() => window.innerWidth < 100")
await page.set_viewport_size({"width": 50, "height": 50})
await watch_dog
await browser.close()
with sync_playwright() as playwright:
run(playwright)
```
To pass an argument to the predicate of `frame.waitForFunction` function: To pass an argument to the predicate of `frame.waitForFunction` function:
```js ```js

View File

@ -916,7 +916,7 @@ Returns the value of the [`param: pageFunction`] invocation as in-page object (J
The only difference between [`method: Page.evaluate`] and [`method: Page.evaluateHandle`] is that [`method: Page.evaluateHandle`] returns in-page The only difference between [`method: Page.evaluate`] and [`method: Page.evaluateHandle`] is that [`method: Page.evaluateHandle`] returns in-page
object (JSHandle). object (JSHandle).
If the function passed to the [`method: Page.evaluateHandle`] returns a [Promise], then [`method:Ppage.EvaluateHandle`] would wait for the If the function passed to the [`method: Page.evaluateHandle`] returns a [Promise], then [`method: Page.evaluateHandle`] would wait for the
promise to resolve and return its value. promise to resolve and return its value.
```js ```js

View File

@ -48,9 +48,6 @@ with sync_playwright() as playwright:
run(playwright) run(playwright)
``` ```
By default, the `playwright` NPM package automatically downloads browser executables during installation. The
`playwright-core` NPM package can be used to skip automatic downloads.
## property: Playwright.chromium ## property: Playwright.chromium
- type: <[BrowserType]> - type: <[BrowserType]>

View File

@ -32,7 +32,7 @@ page.on('requestfailed', request => {
``` ```
```py ```py
page.on("requestfailed", lambda: request => print(request.url + " " + request.failure) page.on("requestfailed", lambda request: print(request.url + " " + request.failure))
``` ```
## method: Request.frame ## method: Request.frame

10
types/types.d.ts vendored
View File

@ -131,7 +131,9 @@ export interface Page {
* *
* If the function passed to the * If the function passed to the
* [page.evaluateHandle()](https://github.com/microsoft/playwright/blob/master/docs/api.md#pageevaluatehandle) returns a * [page.evaluateHandle()](https://github.com/microsoft/playwright/blob/master/docs/api.md#pageevaluatehandle) returns a
* [Promise], then [`method:Ppage.EvaluateHandle`] would wait for the promise to resolve and return its value. * [Promise], then
* [page.evaluateHandle()](https://github.com/microsoft/playwright/blob/master/docs/api.md#pageevaluatehandle) would wait
* for the promise to resolve and return its value.
* *
* ```js * ```js
* const aWindowHandle = await page.evaluateHandle(() => Promise.resolve(window)); * const aWindowHandle = await page.evaluateHandle(() => Promise.resolve(window));
@ -5940,13 +5942,13 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {
* *
* ```js * ```js
* // single selection matching the value * // single selection matching the value
* handle.selectOption('select#colors', 'blue'); * handle.selectOption('blue');
* *
* // single selection matching the label * // single selection matching the label
* handle.selectOption('select#colors', { label: 'Blue' }); * handle.selectOption({ label: 'Blue' });
* *
* // multiple selection * // multiple selection
* handle.selectOption('select#colors', ['red', 'green', 'blue']); * handle.selectOption(['red', 'green', 'blue']);
* ``` * ```
* *
* @param values Options to select. If the `<select>` has the `multiple` attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. String values are equivalent to `{value:'string'}`. Option * @param values Options to select. If the `<select>` has the `multiple` attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. String values are equivalent to `{value:'string'}`. Option