s
- page.get_by_text(re.compile(\"Hello\"))
-
- # Matches second
- page.get_by_text(re.compile(\"^hello$\", re.IGNORECASE))
- ```
-
**Details**
Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into
@@ -16741,10 +15521,6 @@ def get_by_title(
await expect(page.get_by_title(\"Issues count\")).to_have_text(\"25 issues\")
```
- ```py
- expect(page.get_by_title(\"Issues count\")).to_have_text(\"25 issues\")
- ```
-
Parameters
----------
text : Union[Pattern[str], str]
@@ -16773,11 +15549,6 @@ def frame_locator(self, selector: str) -> "FrameLocator":
await locator.click()
```
- ```py
- locator = page.frame_locator(\"iframe\").get_by_text(\"Submit\")
- locator.click()
- ```
-
Parameters
----------
selector : str
@@ -16834,10 +15605,6 @@ def nth(self, index: int) -> "Locator":
banana = await page.get_by_role(\"listitem\").nth(2)
```
- ```py
- banana = page.get_by_role(\"listitem\").nth(2)
- ```
-
Parameters
----------
index : int
@@ -16873,14 +15640,6 @@ def filter(
```
- ```py
- row_locator = page.locator(\"tr\")
- # ...
- row_locator.filter(has_text=\"text in column 1\").filter(
- has=page.get_by_role(\"button\", name=\"column 2 button\")
- ).screenshot()
- ```
-
Parameters
----------
has_text : Union[Pattern[str], str, None]
@@ -16939,15 +15698,6 @@ def or_(self, locator: "Locator") -> "Locator":
await new_email.click()
```
- ```py
- new_email = page.get_by_role(\"button\", name=\"New\")
- dialog = page.get_by_text(\"Confirm security settings\")
- expect(new_email.or_(dialog)).to_be_visible()
- if (dialog.is_visible()):
- page.get_by_role(\"button\", name=\"Dismiss\").click()
- new_email.click()
- ```
-
Parameters
----------
locator : Locator
@@ -16973,10 +15723,6 @@ def and_(self, locator: "Locator") -> "Locator":
button = page.get_by_role(\"button\").and_(page.getByTitle(\"Subscribe\"))
```
- ```py
- button = page.get_by_role(\"button\").and_(page.getByTitle(\"Subscribe\"))
- ```
-
Parameters
----------
locator : Locator
@@ -17035,11 +15781,6 @@ async def all(self) -> typing.List["Locator"]:
await li.click();
```
- ```py
- for li in page.get_by_role('listitem').all():
- li.click();
- ```
-
Returns
-------
List[Locator]
@@ -17061,10 +15802,6 @@ async def count(self) -> int:
count = await page.get_by_role(\"listitem\").count()
```
- ```py
- count = page.get_by_role(\"listitem\").count()
- ```
-
Returns
-------
int
@@ -17107,19 +15844,6 @@ async def drag_to(
)
```
- ```py
- source = page.locator(\"#source\")
- target = page.locator(\"#target\")
-
- source.drag_to(target)
- # or specify exact positions relative to the top-left corners of the elements:
- source.drag_to(
- target,
- source_position={\"x\": 34, \"y\": 7},
- target_position={\"x\": 10, \"y\": 20}
- )
- ```
-
Parameters
----------
target : Locator
@@ -17205,10 +15929,6 @@ async def hover(
await page.get_by_role(\"link\").hover()
```
- ```py
- page.get_by_role(\"link\").hover()
- ```
-
**Details**
This method hovers over the element by performing the following steps:
@@ -17308,10 +16028,6 @@ async def input_value(self, *, timeout: typing.Optional[float] = None) -> str:
value = await page.get_by_role(\"textbox\").input_value()
```
- ```py
- value = page.get_by_role(\"textbox\").input_value()
- ```
-
**Details**
Throws elements that are not an input, textarea or a select. However, if the element is inside the ``
@@ -17348,10 +16064,6 @@ async def is_checked(self, *, timeout: typing.Optional[float] = None) -> bool:
checked = await page.get_by_role(\"checkbox\").is_checked()
```
- ```py
- checked = page.get_by_role(\"checkbox\").is_checked()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -17379,10 +16091,6 @@ async def is_disabled(self, *, timeout: typing.Optional[float] = None) -> bool:
disabled = await page.get_by_role(\"button\").is_disabled()
```
- ```py
- disabled = page.get_by_role(\"button\").is_disabled()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -17412,10 +16120,6 @@ async def is_editable(self, *, timeout: typing.Optional[float] = None) -> bool:
editable = await page.get_by_role(\"textbox\").is_editable()
```
- ```py
- editable = page.get_by_role(\"textbox\").is_editable()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -17445,10 +16149,6 @@ async def is_enabled(self, *, timeout: typing.Optional[float] = None) -> bool:
enabled = await page.get_by_role(\"button\").is_enabled()
```
- ```py
- enabled = page.get_by_role(\"button\").is_enabled()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -17476,10 +16176,6 @@ async def is_hidden(self, *, timeout: typing.Optional[float] = None) -> bool:
hidden = await page.get_by_role(\"button\").is_hidden()
```
- ```py
- hidden = page.get_by_role(\"button\").is_hidden()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -17506,10 +16202,6 @@ async def is_visible(self, *, timeout: typing.Optional[float] = None) -> bool:
visible = await page.get_by_role(\"button\").is_visible()
```
- ```py
- visible = page.get_by_role(\"button\").is_visible()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -17540,10 +16232,6 @@ async def press(
await page.get_by_role(\"textbox\").press(\"Backspace\")
```
- ```py
- page.get_by_role(\"textbox\").press(\"Backspace\")
- ```
-
**Details**
Focuses the element, and then uses `keyboard.down()` and `keyboard.up()`.
@@ -17613,20 +16301,12 @@ async def screenshot(
await page.get_by_role(\"link\").screenshot()
```
- ```py
- page.get_by_role(\"link\").screenshot()
- ```
-
Disable animations and save screenshot to a file:
```py
await page.get_by_role(\"link\").screenshot(animations=\"disabled\", path=\"link.png\")
```
- ```py
- page.get_by_role(\"link\").screenshot(animations=\"disabled\", path=\"link.png\")
- ```
-
**Details**
This method captures a screenshot of the page, clipped to the size and position of a particular element matching
@@ -17772,15 +16452,6 @@ async def select_option(
await element.select_option(value=[\"red\", \"green\", \"blue\"])
```
- ```py
- # single selection matching the value or label
- element.select_option(\"blue\")
- # single selection matching the label
- element.select_option(label=\"blue\")
- # multiple selection for blue, red and second option
- element.select_option(value=[\"red\", \"green\", \"blue\"])
- ```
-
Parameters
----------
value : Union[Sequence[str], str, None]
@@ -17885,24 +16556,6 @@ async def set_input_files(
)
```
- ```py
- # Select one file
- page.get_by_label(\"Upload file\").set_input_files('myfile.pdf')
-
- # Select multiple files
- page.get_by_label(\"Upload files\").set_input_files(['file1.txt', 'file2.txt'])
-
- # Remove all the selected files
- page.get_by_label(\"Upload file\").set_input_files([])
-
- # Upload buffer from memory
- page.get_by_label(\"Upload file\").set_input_files(
- files=[
- {\"name\": \"test.txt\", \"mimeType\": \"text/plain\", \"buffer\": b\"this is a test\"}
- ],
- )
- ```
-
**Details**
Sets the value of the file input to these file paths or files. If some of the `filePaths` are relative paths, then
@@ -18083,11 +16736,6 @@ async def press_sequentially(
await locator.press_sequentially(\"world\", delay=100) # types slower, like a user
```
- ```py
- locator.press_sequentially(\"hello\") # types instantly
- locator.press_sequentially(\"world\", delay=100) # types slower, like a user
- ```
-
An example of typing into a text field and then submitting the form:
```py
@@ -18096,12 +16744,6 @@ async def press_sequentially(
await locator.press(\"Enter\")
```
- ```py
- locator = page.get_by_label(\"Password\")
- locator.press_sequentially(\"my password\")
- locator.press(\"Enter\")
- ```
-
Parameters
----------
text : str
@@ -18142,10 +16784,6 @@ async def uncheck(
await page.get_by_role(\"checkbox\").uncheck()
```
- ```py
- page.get_by_role(\"checkbox\").uncheck()
- ```
-
**Details**
This method unchecks the element by performing the following steps:
@@ -18205,10 +16843,6 @@ async def all_inner_texts(self) -> typing.List[str]:
texts = await page.get_by_role(\"link\").all_inner_texts()
```
- ```py
- texts = page.get_by_role(\"link\").all_inner_texts()
- ```
-
Returns
-------
List[str]
@@ -18230,10 +16864,6 @@ async def all_text_contents(self) -> typing.List[str]:
texts = await page.get_by_role(\"link\").all_text_contents()
```
- ```py
- texts = page.get_by_role(\"link\").all_text_contents()
- ```
-
Returns
-------
List[str]
@@ -18263,11 +16893,6 @@ async def wait_for(
await order_sent.wait_for()
```
- ```py
- order_sent = page.locator(\"#order-sent\")
- order_sent.wait_for()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -18307,10 +16932,6 @@ async def set_checked(
await page.get_by_role(\"checkbox\").set_checked(True)
```
- ```py
- page.get_by_role(\"checkbox\").set_checked(True)
- ```
-
**Details**
This method checks or unchecks an element by performing the following steps:
@@ -18948,14 +17569,6 @@ async def post(
`application/x-www-form-urlencoded` encoding (see below how to use `multipart/form-data` form encoding to send
files):
- ```python
- formData = {
- \"title\": \"Book Title\",
- \"body\": \"John Doe\",
- }
- api_request_context.post(\"https://example.com/api/findBook\", form=formData)
- ```
-
The common way to send file(s) in the body of a request is to upload them as form fields with `multipart/form-data`
encoding. You can achieve that with Playwright API like this:
@@ -19063,19 +17676,6 @@ async def fetch(
The common way to send file(s) in the body of a request is to encode it as form fields with `multipart/form-data`
encoding. You can achieve that with Playwright API like this:
- ```python
- api_request_context.fetch(
- \"https://example.com/api/uploadScrip'\",
- method=\"post\",
- multipart={
- \"fileField\": {
- \"name\": \"f.js\",
- \"mimeType\": \"text/javascript\",
- \"buffer\": b\"console.log(2022);\",
- },
- })
- ```
-
Parameters
----------
url_or_request : Union[Request, str]
@@ -19252,14 +17852,6 @@ async def to_have_title(
await expect(page).to_have_title(re.compile(r\".*checkout\"))
```
- ```py
- import re
- from playwright.sync_api import expect
-
- # ...
- expect(page).to_have_title(re.compile(r\".*checkout\"))
- ```
-
Parameters
----------
title_or_reg_exp : Union[Pattern[str], str]
@@ -19320,14 +17912,6 @@ async def to_have_url(
await expect(page).to_have_url(re.compile(\".*checkout\"))
```
- ```py
- import re
- from playwright.sync_api import expect
-
- # ...
- expect(page).to_have_url(re.compile(\".*checkout\"))
- ```
-
Parameters
----------
url_or_reg_exp : Union[Pattern[str], str]
@@ -19408,15 +17992,6 @@ async def to_contain_text(
await expect(locator).to_contain_text(re.compile(r\"\\d messages\"))
```
- ```py
- import re
- from playwright.sync_api import expect
-
- locator = page.locator('.title')
- expect(locator).to_contain_text(\"substring\")
- expect(locator).to_contain_text(re.compile(r\"\\d messages\"))
- ```
-
If you pass an array as an expected value, the expectations are:
1. Locator resolves to a list of elements.
1. Elements from a **subset** of this list contain text from the expected array, respectively.
@@ -19451,22 +18026,6 @@ async def to_contain_text(
await expect(page.locator(\"ul\")).to_contain_text([\"Text 3\"])
```
- ```py
- from playwright.sync_api import expect
-
- # ✓ Contains the right items in the right order
- expect(page.locator(\"ul > li\")).to_contain_text([\"Text 1\", \"Text 3\", \"Text 4\"])
-
- # ✖ Wrong order
- expect(page.locator(\"ul > li\")).to_contain_text([\"Text 3\", \"Text 2\"])
-
- # ✖ No item contains this text
- expect(page.locator(\"ul > li\")).to_contain_text([\"Some 33\"])
-
- # ✖ Locator points to the outer list element, not to the list items
- expect(page.locator(\"ul\")).to_contain_text([\"Text 3\"])
- ```
-
Parameters
----------
expected : Union[Pattern[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]], Sequence[str], str]
@@ -19552,13 +18111,6 @@ async def to_have_attribute(
await expect(locator).to_have_attribute(\"type\", \"text\")
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"input\")
- expect(locator).to_have_attribute(\"type\", \"text\")
- ```
-
Parameters
----------
name : str
@@ -19642,14 +18194,6 @@ async def to_have_class(
await expect(locator).to_have_class(\"selected row\")
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"#component\")
- expect(locator).to_have_class(re.compile(r\"selected\"))
- expect(locator).to_have_class(\"selected row\")
- ```
-
Note that if array is passed as an expected value, entire lists of elements can be asserted:
```py
@@ -19659,13 +18203,6 @@ async def to_have_class(
await expect(locator).to_have_class([\"component\", \"component selected\", \"component\"])
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"list > .component\")
- expect(locator).to_have_class([\"component\", \"component selected\", \"component\"])
- ```
-
Parameters
----------
expected : Union[Pattern[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]], Sequence[str], str]
@@ -19728,13 +18265,6 @@ async def to_have_count(
await expect(locator).to_have_count(3)
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"list > .component\")
- expect(locator).to_have_count(3)
- ```
-
Parameters
----------
count : int
@@ -19788,13 +18318,6 @@ async def to_have_css(
await expect(locator).to_have_css(\"display\", \"flex\")
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.get_by_role(\"button\")
- expect(locator).to_have_css(\"display\", \"flex\")
- ```
-
Parameters
----------
name : str
@@ -19857,13 +18380,6 @@ async def to_have_id(
await expect(locator).to_have_id(\"lastname\")
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.get_by_role(\"textbox\")
- expect(locator).to_have_id(\"lastname\")
- ```
-
Parameters
----------
id : Union[Pattern[str], str]
@@ -19917,13 +18433,6 @@ async def to_have_js_property(
await expect(locator).to_have_js_property(\"loaded\", True)
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\".component\")
- expect(locator).to_have_js_property(\"loaded\", True)
- ```
-
Parameters
----------
name : str
@@ -19986,14 +18495,6 @@ async def to_have_value(
await expect(locator).to_have_value(re.compile(r\"[0-9]\"))
```
- ```py
- import re
- from playwright.sync_api import expect
-
- locator = page.locator(\"input[type=number]\")
- expect(locator).to_have_value(re.compile(r\"[0-9]\"))
- ```
-
Parameters
----------
value : Union[Pattern[str], str]
@@ -20066,15 +18567,6 @@ async def to_have_values(
await expect(locator).to_have_values([re.compile(r\"R\"), re.compile(r\"G\")])
```
- ```py
- import re
- from playwright.sync_api import expect
-
- locator = page.locator(\"id=favorite-colors\")
- locator.select_option([\"R\", \"G\"])
- expect(locator).to_have_values([re.compile(r\"R\"), re.compile(r\"G\")])
- ```
-
Parameters
----------
values : Union[Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]], Sequence[str]]
@@ -20154,15 +18646,6 @@ async def to_have_text(
await expect(locator).to_have_text(re.compile(r\"Welcome, .*\"))
```
- ```py
- import re
- from playwright.sync_api import expect
-
- locator = page.locator(\".title\")
- expect(locator).to_have_text(re.compile(r\"Welcome, Test User\"))
- expect(locator).to_have_text(re.compile(r\"Welcome, .*\"))
- ```
-
If you pass an array as an expected value, the expectations are:
1. Locator resolves to a list of elements.
1. The number of elements equals the number of expected values in the array.
@@ -20196,22 +18679,6 @@ async def to_have_text(
await expect(page.locator(\"ul\")).to_have_text([\"Text 1\", \"Text 2\", \"Text 3\"])
```
- ```py
- from playwright.sync_api import expect
-
- # ✓ Has the right items in the right order
- expect(page.locator(\"ul > li\")).to_have_text([\"Text 1\", \"Text 2\", \"Text 3\"])
-
- # ✖ Wrong order
- expect(page.locator(\"ul > li\")).to_have_text([\"Text 3\", \"Text 2\", \"Text 1\"])
-
- # ✖ Last item does not match
- expect(page.locator(\"ul > li\")).to_have_text([\"Text 1\", \"Text 2\", \"Text\"])
-
- # ✖ Locator points to the outer list element, not to the list items
- expect(page.locator(\"ul\")).to_have_text([\"Text 1\", \"Text 2\", \"Text 3\"])
- ```
-
Parameters
----------
expected : Union[Pattern[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]], Sequence[str], str]
@@ -20293,10 +18760,6 @@ async def to_be_attached(
await expect(page.get_by_text(\"Hidden text\")).to_be_attached()
```
- ```py
- expect(page.get_by_text(\"Hidden text\")).to_be_attached()
- ```
-
Parameters
----------
attached : Union[bool, None]
@@ -20328,13 +18791,6 @@ async def to_be_checked(
await expect(locator).to_be_checked()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.get_by_label(\"Subscribe to newsletter\")
- expect(locator).to_be_checked()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -20405,13 +18861,6 @@ async def to_be_disabled(self, *, timeout: typing.Optional[float] = None) -> Non
await expect(locator).to_be_disabled()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"button.submit\")
- expect(locator).to_be_disabled()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -20460,13 +18909,6 @@ async def to_be_editable(
await expect(locator).to_be_editable()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.get_by_role(\"textbox\")
- expect(locator).to_be_editable()
- ```
-
Parameters
----------
editable : Union[bool, None]
@@ -20515,13 +18957,6 @@ async def to_be_empty(self, *, timeout: typing.Optional[float] = None) -> None:
await expect(locator).to_be_empty()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"div.warning\")
- expect(locator).to_be_empty()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -20568,13 +19003,6 @@ async def to_be_enabled(
await expect(locator).to_be_enabled()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator(\"button.submit\")
- expect(locator).to_be_enabled()
- ```
-
Parameters
----------
enabled : Union[bool, None]
@@ -20624,13 +19052,6 @@ async def to_be_hidden(self, *, timeout: typing.Optional[float] = None) -> None:
await expect(locator).to_be_hidden()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.locator('.my-element')
- expect(locator).to_be_hidden()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -20687,21 +19108,6 @@ async def to_be_visible(
).to_be_visible()
```
- ```py
- # A specific element is visible.
- expect(page.get_by_text(\"Welcome\")).to_be_visible()
-
- # At least one item in the list is visible.
- expect(page.get_by_test_id(\"todo-item\").first).to_be_visible()
-
- # At least one of the two elements is visible, possibly both.
- expect(
- page.get_by_role(\"button\", name=\"Sign in\")
- .or_(page.get_by_role(\"button\", name=\"Sign up\"))
- .first
- ).to_be_visible()
- ```
-
Parameters
----------
visible : Union[bool, None]
@@ -20750,13 +19156,6 @@ async def to_be_focused(self, *, timeout: typing.Optional[float] = None) -> None
await expect(locator).to_be_focused()
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.get_by_role(\"textbox\")
- expect(locator).to_be_focused()
- ```
-
Parameters
----------
timeout : Union[float, None]
@@ -20811,18 +19210,6 @@ async def to_be_in_viewport(
await expect(locator).to_be_in_viewport(ratio=0.5)
```
- ```py
- from playwright.sync_api import expect
-
- locator = page.get_by_role(\"button\")
- # Make sure at least some part of element intersects viewport.
- expect(locator).to_be_in_viewport()
- # Make sure element is fully outside of viewport.
- expect(locator).not_to_be_in_viewport()
- # Make sure that at least half of the element intersects viewport.
- expect(locator).to_be_in_viewport(ratio=0.5)
- ```
-
Parameters
----------
ratio : Union[float, None]
@@ -20877,14 +19264,6 @@ async def to_be_ok(self) -> None:
# ...
await expect(response).to_be_ok()
```
-
- ```py
- import re
- from playwright.sync_api import expect
-
- # ...
- expect(response).to_be_ok()
- ```
"""
__tracebackhide__ = True
diff --git a/playwright/sync_api/_generated.py b/playwright/sync_api/_generated.py
index d64175f4f..34e54b313 100644
--- a/playwright/sync_api/_generated.py
+++ b/playwright/sync_api/_generated.py
@@ -205,11 +205,6 @@ def redirected_from(self) -> typing.Optional["Request"]:
For example, if the website `http://example.com` redirects to `https://example.com`:
- ```py
- response = await page.goto(\"http://example.com\")
- print(response.request.redirected_from.url) # \"http://example.com\"
- ```
-
```py
response = page.goto(\"http://example.com\")
print(response.request.redirected_from.url) # \"http://example.com\"
@@ -217,11 +212,6 @@ def redirected_from(self) -> typing.Optional["Request"]:
If the website `https://google.com` has no redirects:
- ```py
- response = await page.goto(\"https://google.com\")
- print(response.request.redirected_from) # None
- ```
-
```py
response = page.goto(\"https://google.com\")
print(response.request.redirected_from) # None
@@ -283,13 +273,6 @@ def timing(self) -> ResourceTiming:
**Usage**
- ```py
- async with page.expect_event(\"requestfinished\") as request_info:
- await page.goto(\"http://example.com\")
- request = await request_info.value
- print(request.timing)
- ```
-
```py
with page.expect_event(\"requestfinished\") as request_info:
page.goto(\"http://example.com\")
@@ -708,13 +691,6 @@ def fulfill(
An example of fulfilling all requests with 404 responses:
- ```py
- await page.route(\"**/*\", lambda route: route.fulfill(
- status=404,
- content_type=\"text/plain\",
- body=\"not found!\"))
- ```
-
```py
page.route(\"**/*\", lambda route: route.fulfill(
status=404,
@@ -724,10 +700,6 @@ def fulfill(
An example of serving static file:
- ```py
- await page.route(\"**/xhr_endpoint\", lambda route: route.fulfill(path=\"mock_data.json\"))
- ```
-
```py
page.route(\"**/xhr_endpoint\", lambda route: route.fulfill(path=\"mock_data.json\"))
```
@@ -783,16 +755,6 @@ def fetch(
**Usage**
- ```py
- async def handle(route):
- response = await route.fetch()
- json = await response.json()
- json[\"message\"][\"big_red_dog\"] = []
- await route.fulfill(response=response, json=json)
-
- await page.route(\"https://dog.ceo/api/breeds/list/all\", handle)
- ```
-
```py
def handle(route):
response = route.fetch()
@@ -862,12 +824,6 @@ def fallback(
**Usage**
- ```py
- await page.route(\"**/*\", lambda route: route.abort()) # Runs last.
- await page.route(\"**/*\", lambda route: route.fallback()) # Runs second.
- await page.route(\"**/*\", lambda route: route.fallback()) # Runs first.
- ```
-
```py
page.route(\"**/*\", lambda route: route.abort()) # Runs last.
page.route(\"**/*\", lambda route: route.fallback()) # Runs second.
@@ -877,27 +833,6 @@ def fallback(
Registering multiple routes is useful when you want separate handlers to handle different kinds of requests, for
example API calls vs page resources or GET requests vs POST requests as in the example below.
- ```py
- # Handle GET requests.
- def handle_get(route):
- if route.request.method != \"GET\":
- route.fallback()
- return
- # Handling GET only.
- # ...
-
- # Handle POST requests.
- def handle_post(route):
- if route.request.method != \"POST\":
- route.fallback()
- return
- # Handling POST only.
- # ...
-
- await page.route(\"**/*\", handle_get)
- await page.route(\"**/*\", handle_post)
- ```
-
```py
# Handle GET requests.
def handle_get(route):
@@ -922,19 +857,6 @@ def handle_post(route):
One can also modify request while falling back to the subsequent handler, that way intermediate route handler can
modify url, method, headers and postData of the request.
- ```py
- async def handle(route, request):
- # override headers
- headers = {
- **request.headers,
- \"foo\": \"foo-value\", # set \"foo\" header
- \"bar\": None # remove \"bar\" header
- }
- await route.fallback(headers=headers)
-
- await page.route(\"**/*\", handle)
- ```
-
```py
def handle(route, request):
# override headers
@@ -986,19 +908,6 @@ def continue_(
**Usage**
- ```py
- async def handle(route, request):
- # override headers
- headers = {
- **request.headers,
- \"foo\": \"foo-value\", # set \"foo\" header
- \"bar\": None # remove \"bar\" header
- }
- await route.continue_(headers=headers)
-
- await page.route(\"**/*\", handle)
- ```
-
```py
def handle(route, request):
# override headers
@@ -1276,10 +1185,6 @@ def insert_text(self, text: str) -> None:
**Usage**
- ```py
- await page.keyboard.insert_text(\"嗨\")
- ```
-
```py
page.keyboard.insert_text(\"嗨\")
```
@@ -1309,11 +1214,6 @@ def type(self, text: str, *, delay: typing.Optional[float] = None) -> None:
**Usage**
- ```py
- await page.keyboard.type(\"Hello\") # types instantly
- await page.keyboard.type(\"World\", delay=100) # types slower, like a user
- ```
-
```py
page.keyboard.type(\"Hello\") # types instantly
page.keyboard.type(\"World\", delay=100) # types slower, like a user
@@ -1361,18 +1261,6 @@ def press(self, key: str, *, delay: typing.Optional[float] = None) -> None:
**Usage**
- ```py
- page = await browser.new_page()
- await page.goto(\"https://keycode.info\")
- await page.keyboard.press(\"a\")
- await page.screenshot(path=\"a.png\")
- await page.keyboard.press(\"ArrowLeft\")
- await page.screenshot(path=\"arrow_left.png\")
- await page.keyboard.press(\"Shift+O\")
- await page.screenshot(path=\"o.png\")
- await browser.close()
- ```
-
```py
page = browser.new_page()
page.goto(\"https://keycode.info\")
@@ -1584,11 +1472,6 @@ def evaluate(
**Usage**
- ```py
- tweet_handle = await page.query_selector(\".tweet .retweets\")
- assert await tweet_handle.evaluate(\"node => node.innerText\") == \"10 retweets\"
- ```
-
```py
tweet_handle = page.query_selector(\".tweet .retweets\")
assert tweet_handle.evaluate(\"node => node.innerText\") == \"10 retweets\"
@@ -1677,14 +1560,6 @@ def get_properties(self) -> typing.Dict[str, "JSHandle"]:
**Usage**
- ```py
- handle = await page.evaluate_handle(\"({ window, document })\")
- properties = await handle.get_properties()
- window_handle = properties.get(\"window\")
- document_handle = properties.get(\"document\")
- await handle.dispose()
- ```
-
```py
handle = page.evaluate_handle(\"({ window, document })\")
properties = handle.get_properties()
@@ -1914,10 +1789,6 @@ def dispatch_event(
**Usage**
- ```py
- await element_handle.dispatch_event(\"click\")
- ```
-
```py
element_handle.dispatch_event(\"click\")
```
@@ -1939,12 +1810,6 @@ def dispatch_event(
You can also specify `JSHandle` as the property value if you want live objects to be passed into the event:
- ```py
- # note you can only create data_transfer in chromium and firefox
- data_transfer = await page.evaluate_handle(\"new DataTransfer()\")
- await element_handle.dispatch_event(\"#source\", \"dragstart\", {\"dataTransfer\": data_transfer})
- ```
-
```py
# note you can only create data_transfer in chromium and firefox
data_transfer = page.evaluate_handle(\"new DataTransfer()\")
@@ -2222,15 +2087,6 @@ def select_option(
**Usage**
- ```py
- # Single selection matching the value or label
- await handle.select_option(\"blue\")
- # single selection matching the label
- await handle.select_option(label=\"blue\")
- # multiple selection
- await handle.select_option(value=[\"red\", \"green\", \"blue\"])
- ```
-
```py
# Single selection matching the value or label
handle.select_option(\"blue\")
@@ -2774,11 +2630,6 @@ def bounding_box(self) -> typing.Optional[FloatRect]:
**Usage**
- ```py
- box = await element_handle.bounding_box()
- await page.mouse.click(box[\"x\"] + box[\"width\"] / 2, box[\"y\"] + box[\"height\"] / 2)
- ```
-
```py
box = element_handle.bounding_box()
page.mouse.click(box[\"x\"] + box[\"width\"] / 2, box[\"y\"] + box[\"height\"] / 2)
@@ -2938,12 +2789,6 @@ def eval_on_selector(
**Usage**
- ```py
- tweet_handle = await page.query_selector(\".tweet\")
- assert await tweet_handle.eval_on_selector(\".like\", \"node => node.innerText\") == \"100\"
- assert await tweet_handle.eval_on_selector(\".retweets\", \"node => node.innerText\") == \"10\"
- ```
-
```py
tweet_handle = page.query_selector(\".tweet\")
assert tweet_handle.eval_on_selector(\".like\", \"node => node.innerText\") == \"100\"
@@ -2995,11 +2840,6 @@ def eval_on_selector_all(
```
- ```py
- feed_handle = await page.query_selector(\".feed\")
- assert await feed_handle.eval_on_selector_all(\".tweet\", \"nodes => nodes.map(n => n.innerText)\") == [\"hello!\", \"hi!\"]
- ```
-
```py
feed_handle = page.query_selector(\".feed\")
assert feed_handle.eval_on_selector_all(\".tweet\", \"nodes => nodes.map(n => n.innerText)\") == [\"hello!\", \"hi!\"]
@@ -3090,13 +2930,6 @@ def wait_for_selector(
**Usage**
- ```py
- await page.set_content(\"
\")
- div = await page.query_selector(\"div\")
- # waiting for the \"span\" selector relative to the div.
- span = await div.wait_for_selector(\"span\", state=\"attached\")
- ```
-
```py
page.set_content(\"
\")
div = page.query_selector(\"div\")
@@ -3162,11 +2995,6 @@ def snapshot(
An example of dumping the entire accessibility tree:
- ```py
- snapshot = await page.accessibility.snapshot()
- print(snapshot)
- ```
-
```py
snapshot = page.accessibility.snapshot()
print(snapshot)
@@ -3174,22 +3002,6 @@ def snapshot(
An example of logging the focused node's name:
- ```py
- def find_focused_node(node):
- if node.get(\"focused\"):
- return node
- for child in (node.get(\"children\") or []):
- found_node = find_focused_node(child)
- if found_node:
- return found_node
- return None
-
- snapshot = await page.accessibility.snapshot()
- node = find_focused_node(snapshot)
- if node:
- print(node[\"name\"])
- ```
-
```py
def find_focused_node(node):
if node.get(\"focused\"):
@@ -3463,12 +3275,6 @@ def expect_navigation(
This method waits for the frame to navigate to a new URL. It is useful for when you run code which will indirectly
cause the frame to navigate. Consider this example:
- ```py
- async with frame.expect_navigation():
- await frame.click(\"a.delayed-navigation\") # clicking the link will indirectly cause a navigation
- # Resolves after navigation has finished
- ```
-
```py
with frame.expect_navigation():
frame.click(\"a.delayed-navigation\") # clicking the link will indirectly cause a navigation
@@ -3524,11 +3330,6 @@ def wait_for_url(
**Usage**
- ```py
- await frame.click(\"a.delayed-navigation\") # clicking the link will indirectly cause a navigation
- await frame.wait_for_url(\"**/target.html\")
- ```
-
```py
frame.click(\"a.delayed-navigation\") # clicking the link will indirectly cause a navigation
frame.wait_for_url(\"**/target.html\")
@@ -3581,11 +3382,6 @@ def wait_for_load_state(
**Usage**
- ```py
- await frame.click(\"button\") # click triggers navigation.
- await frame.wait_for_load_state() # the promise resolves after \"load\" event.
- ```
-
```py
frame.click(\"button\") # click triggers navigation.
frame.wait_for_load_state() # the promise resolves after \"load\" event.
@@ -3623,12 +3419,6 @@ def frame_element(self) -> "ElementHandle":
**Usage**
- ```py
- frame_element = await frame.frame_element()
- content_frame = await frame_element.content_frame()
- assert frame == content_frame
- ```
-
```py
frame_element = frame.frame_element()
content_frame = frame_element.content_frame()
@@ -3658,11 +3448,6 @@ def evaluate(
**Usage**
- ```py
- result = await frame.evaluate(\"([x, y]) => Promise.resolve(x * y)\", [7, 8])
- print(result) # prints \"56\"
- ```
-
```py
result = frame.evaluate(\"([x, y]) => Promise.resolve(x * y)\", [7, 8])
print(result) # prints \"56\"
@@ -3670,12 +3455,6 @@ def evaluate(
A string can also be passed in instead of a function.
- ```py
- print(await frame.evaluate(\"1 + 2\")) # prints \"3\"
- x = 10
- print(await frame.evaluate(f\"1 + {x}\")) # prints \"11\"
- ```
-
```py
print(frame.evaluate(\"1 + 2\")) # prints \"3\"
x = 10
@@ -3684,12 +3463,6 @@ def evaluate(
`ElementHandle` instances can be passed as an argument to the `frame.evaluate()`:
- ```py
- body_handle = await frame.evaluate(\"document.body\")
- html = await frame.evaluate(\"([body, suffix]) => body.innerHTML + suffix\", [body_handle, \"hello\"])
- await body_handle.dispose()
- ```
-
```py
body_handle = frame.evaluate(\"document.body\")
html = frame.evaluate(\"([body, suffix]) => body.innerHTML + suffix\", [body_handle, \"hello\"])
@@ -3730,11 +3503,6 @@ def evaluate_handle(
**Usage**
- ```py
- a_window_handle = await frame.evaluate_handle(\"Promise.resolve(window)\")
- a_window_handle # handle for the window object.
- ```
-
```py
a_window_handle = frame.evaluate_handle(\"Promise.resolve(window)\")
a_window_handle # handle for the window object.
@@ -3742,23 +3510,12 @@ def evaluate_handle(
A string can also be passed in instead of a function.
- ```py
- a_handle = await page.evaluate_handle(\"document\") # handle for the \"document\"
- ```
-
```py
a_handle = page.evaluate_handle(\"document\") # handle for the \"document\"
```
`JSHandle` instances can be passed as an argument to the `frame.evaluate_handle()`:
- ```py
- a_handle = await page.evaluate_handle(\"document.body\")
- result_handle = await page.evaluate_handle(\"body => body.innerHTML\", a_handle)
- print(await result_handle.json_value())
- await result_handle.dispose()
- ```
-
```py
a_handle = page.evaluate_handle(\"document.body\")
result_handle = page.evaluate_handle(\"body => body.innerHTML\", a_handle)
@@ -3866,26 +3623,6 @@ def wait_for_selector(
This method works across navigations:
- ```py
- import asyncio
- from playwright.async_api import async_playwright, Playwright
-
- async def run(playwright: Playwright):
- chromium = playwright.chromium
- browser = await chromium.launch()
- page = await browser.new_page()
- for current_url in [\"https://google.com\", \"https://bbc.com\"]:
- await page.goto(current_url, wait_until=\"domcontentloaded\")
- element = await page.main_frame.wait_for_selector(\"img\")
- print(\"Loaded image: \" + str(await element.get_attribute(\"src\")))
- await browser.close()
-
- async def main():
- async with async_playwright() as playwright:
- await run(playwright)
- asyncio.run(main())
- ```
-
```py
from playwright.sync_api import sync_playwright, Playwright
@@ -4168,10 +3905,6 @@ def dispatch_event(
**Usage**
- ```py
- await frame.dispatch_event(\"button#submit\", \"click\")
- ```
-
```py
frame.dispatch_event(\"button#submit\", \"click\")
```
@@ -4193,12 +3926,6 @@ def dispatch_event(
You can also specify `JSHandle` as the property value if you want live objects to be passed into the event:
- ```py
- # note you can only create data_transfer in chromium and firefox
- data_transfer = await frame.evaluate_handle(\"new DataTransfer()\")
- await frame.dispatch_event(\"#source\", \"dragstart\", { \"dataTransfer\": data_transfer })
- ```
-
```py
# note you can only create data_transfer in chromium and firefox
data_transfer = frame.evaluate_handle(\"new DataTransfer()\")
@@ -4254,12 +3981,6 @@ def eval_on_selector(
**Usage**
- ```py
- search_value = await frame.eval_on_selector(\"#search\", \"el => el.value\")
- preload_href = await frame.eval_on_selector(\"link[rel=preload]\", \"el => el.href\")
- html = await frame.eval_on_selector(\".main-container\", \"(e, suffix) => e.outerHTML + suffix\", \"hello\")
- ```
-
```py
search_value = frame.eval_on_selector(\"#search\", \"el => el.value\")
preload_href = frame.eval_on_selector(\"link[rel=preload]\", \"el => el.href\")
@@ -4310,10 +4031,6 @@ def eval_on_selector_all(
**Usage**
- ```py
- divs_counts = await frame.eval_on_selector_all(\"div\", \"(divs, min) => divs.length >= min\", 10)
- ```
-
```py
divs_counts = frame.eval_on_selector_all(\"div\", \"(divs, min) => divs.length >= min\", 10)
```
@@ -4852,10 +4569,6 @@ def get_by_alt_text(
```
- ```py
- await page.get_by_alt_text(\"Playwright logo\").click()
- ```
-
```py
page.get_by_alt_text(\"Playwright logo\").click()
```
@@ -4896,11 +4609,6 @@ def get_by_label(
```
- ```py
- await page.get_by_label(\"Username\").fill(\"john\")
- await page.get_by_label(\"Password\").fill(\"secret\")
- ```
-
```py
page.get_by_label(\"Username\").fill(\"john\")
page.get_by_label(\"Password\").fill(\"secret\")
@@ -4941,10 +4649,6 @@ def get_by_placeholder(
You can fill the input after locating it by the placeholder text:
- ```py
- await page.get_by_placeholder(\"name@example.com\").fill(\"playwright@microsoft.com\")
- ```
-
```py
page.get_by_placeholder(\"name@example.com\").fill(\"playwright@microsoft.com\")
```
@@ -5084,14 +4788,6 @@ def get_by_role(
You can locate each element by it's implicit role:
- ```py
- await expect(page.get_by_role(\"heading\", name=\"Sign up\")).to_be_visible()
-
- await page.get_by_role(\"checkbox\", name=\"Subscribe\").check()
-
- await page.get_by_role(\"button\", name=re.compile(\"submit\", re.IGNORECASE)).click()
- ```
-
```py
expect(page.get_by_role(\"heading\", name=\"Sign up\")).to_be_visible()
@@ -5191,10 +4887,6 @@ def get_by_test_id(
You can locate the element by it's test id:
- ```py
- await page.get_by_test_id(\"directions\").click()
- ```
-
```py
page.get_by_test_id(\"directions\").click()
```
@@ -5257,23 +4949,6 @@ def get_by_text(
page.get_by_text(re.compile(\"^hello$\", re.IGNORECASE))
```
- ```py
- # Matches
- page.get_by_text(\"world\")
-
- # Matches first
- page.get_by_text(\"Hello world\")
-
- # Matches second
- page.get_by_text(\"Hello\", exact=True)
-
- # Matches both
s
- page.get_by_text(re.compile(\"Hello\"))
-
- # Matches second
- page.get_by_text(re.compile(\"^hello$\", re.IGNORECASE))
- ```
-
**Details**
Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into
@@ -5317,10 +4992,6 @@ def get_by_title(
You can check the issues count after locating it by the title text:
- ```py
- await expect(page.get_by_title(\"Issues count\")).to_have_text(\"25 issues\")
- ```
-
```py
expect(page.get_by_title(\"Issues count\")).to_have_text(\"25 issues\")
```
@@ -5351,11 +5022,6 @@ def frame_locator(self, selector: str) -> "FrameLocator":
Following snippet locates element with text \"Submit\" in the iframe with id `my-frame`, like `