Page
conatus.utils.browser.page
¶
Page module.
This module is used to manage a Playwright page. Think of a page as a tab in a
browser. This module is used in conjunction with the BrowserContext module. The
BrowserContext can have one or multiple pages which share the same context,
such as cookies, etc.
The Page module is used to manage a single page. It is used to navigate to
URLs, wait for the page to load, etc. Each time a navigation action occurs, a
new Step is created.
Configuration¶
You can configure some parameters for the page:
-
Timeout settings: Control how long you're comfortable waiting for the page to load.
hard_timeout: The hard timeout for the page, as encoded in Playwright. Default is 20000 ms (20 seconds).soft_timeout: The soft timeout for the page, which is a looser constraint that we manage. Default is 10000 ms (10 seconds).time_delta: The time delta at which we check for page stability. Default is 0.5 seconds.stability_threshold: The threshold for page stability. We consider the page stable if the DOM hasn't changed for this amount of time. Default is 0.99 seconds.
Note
You have to pass a full BrowserConfig instance during
instantiation of the page, not just the page configuration.
MouseButton
module-attribute
¶
Type for mouse buttons.
ExtendedMouseButton
module-attribute
¶
Type for mouse buttons, with back and forward.
ScrollDirection
module-attribute
¶
Type for scroll directions.
Page
¶
Page(
bc: BrowserContext,
config: BrowserConfig,
pw_page: Page | None = None,
*,
async_init: Literal[False] = False
)
Page(
bc: None = None,
config: None = None,
pw_page: None = None,
*,
async_init: Literal[True] = True
)
Page(
bc: BrowserContext | None = None,
config: BrowserConfig | None = None,
pw_page: Page | None = None,
*,
async_init: bool = False
)
Page module.
Init in sync or async context¶
You can initialize the page in a sync or async context:
from conatus.utils.browser.page import Page
from conatus.utils.browser.full import FullBrowser
browser = FullBrowser(headless=True)
context = browser.contexts[0]
# Sync context
page = Page(context, browser.config)
# Async context
# page = await Page.init_async(context, browser.config)
| PARAMETER | DESCRIPTION |
|---|---|
bc
|
The browser context.
TYPE:
|
config
|
The browser configuration.
TYPE:
|
pw_page
|
The Playwright page instance. If provided, we will use this page instance instead of creating a new one.
TYPE:
|
async_init
|
Whether this function is called in an async context. Users should not set this parameter. Defaults to False.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in conatus/utils/browser/page.py
mouse_position
instance-attribute
¶
The current mouse position.
Only recorded after actions
This is not updated in real-time. It is only updated when we take an action.
init_async
async
classmethod
¶
init_async(
bc: BrowserContext,
config: BrowserConfig,
pw_page: Page | None = None,
) -> Page
Create a page in async mode.
| PARAMETER | DESCRIPTION |
|---|---|
bc
|
The browser context.
TYPE:
|
config
|
The browser configuration.
TYPE:
|
pw_page
|
The Playwright page instance. If provided, we will use this page instance instead of creating a new one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Page
|
The |
Source code in conatus/utils/browser/page.py
close_async
async
¶
Close the page in an async context.
You should also read the documentation for close
.
close
¶
Close the page.
Warning
Please try to ensure that, if you use this method, you also remove the page from the context's list of pages.
Source code in conatus/utils/browser/page.py
wait_for_load_async
async
¶
Waits for the page to load in an async context.
Note that everything in this function is expressed in seconds.
Source code in conatus/utils/browser/page.py
wait_for_load
¶
Wait for the page to load.
Will throw ValueError if the CDP client is not initialized.
Source code in conatus/utils/browser/page.py
goto_async
async
¶
goto_async(url: str) -> None
Go to a URL in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
url
|
The URL to go to.
TYPE:
|
Source code in conatus/utils/browser/page.py
mouse_move_async
async
¶
Move the mouse to a position in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The x coordinate to move the mouse to.
TYPE:
|
y
|
The y coordinate to move the mouse to.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
mouse_down_up_async
async
¶
mouse_down_up_async(
event: Literal["down", "up"],
*,
x: float | None = None,
y: float | None = None,
button: MouseButton = "left",
wait_for_load: bool = True
) -> None
Mouse down or up on an element in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
event
|
The event to perform. Can be
TYPE:
|
x
|
The x coordinate to click on.
TYPE:
|
y
|
The y coordinate to click on.
TYPE:
|
button
|
The button to click on.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if |
Source code in conatus/utils/browser/page.py
mouse_down_up
¶
mouse_down_up(
event: Literal["down", "up"],
*,
x: float | None = None,
y: float | None = None,
button: MouseButton = "left",
wait_for_load: bool = True
) -> None
Mouse down or up on an element.
| PARAMETER | DESCRIPTION |
|---|---|
event
|
The event to perform. Can be
TYPE:
|
x
|
The x coordinate to click on.
TYPE:
|
y
|
The y coordinate to click on.
TYPE:
|
button
|
The button to click on.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
click_async
async
¶
click_async(
x: float,
y: float,
*,
button: ExtendedMouseButton = "left",
click_count: int = 1,
wait_for_load: bool = True
) -> None
Click on an element in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The x coordinate to click on.
TYPE:
|
y
|
The y coordinate to click on.
TYPE:
|
button
|
The button to click on.
TYPE:
|
click_count
|
The number of clicks to perform.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
click
¶
click(
x: float,
y: float,
*,
button: ExtendedMouseButton = "left",
click_count: int = 1,
wait_for_load: bool = True
) -> None
Click on an element.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The x coordinate to click on.
TYPE:
|
y
|
The y coordinate to click on.
TYPE:
|
button
|
The button to click on.
TYPE:
|
click_count
|
The number of clicks to perform.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
scroll_precise_async
async
¶
scroll_precise_async(
delta_x: float,
delta_y: float,
*,
start_x: float | None = None,
start_y: float | None = None,
wait_for_load: bool = True
) -> None
Scroll the page precisely in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
delta_x
|
The x coordinate of the scroll distance.
TYPE:
|
delta_y
|
The y coordinate of the scroll distance.
TYPE:
|
start_x
|
The x coordinate to start scrolling from.
TYPE:
|
start_y
|
The y coordinate to start scrolling from.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if |
Source code in conatus/utils/browser/page.py
scroll_precise
¶
scroll_precise(
delta_x: float,
delta_y: float,
*,
start_x: float | None = None,
start_y: float | None = None,
wait_for_load: bool = True
) -> None
Scroll the page precisely.
| PARAMETER | DESCRIPTION |
|---|---|
start_x
|
The x coordinate to start scrolling from.
TYPE:
|
start_y
|
The y coordinate to start scrolling from.
TYPE:
|
delta_x
|
The x coordinate of the scroll distance.
TYPE:
|
delta_y
|
The y coordinate of the scroll distance.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
scroll_async
async
¶
scroll_async(
direction: ScrollDirection,
magnitude_px: int | None = None,
magnitude_relative: float | None = None,
*,
wait_for_load: bool = True
) -> None
Scroll the page in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
direction
|
The direction to scroll. Can be
TYPE:
|
magnitude_px
|
The magnitude to scroll. If
TYPE:
|
magnitude_relative
|
The magnitude to scroll relative to the
current viewport size. If
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if the viewport size is None (should never happen.) |
ValueError
|
if the direction is invalid. |
Source code in conatus/utils/browser/page.py
scroll
¶
scroll(
direction: ScrollDirection,
height: int | None = None,
*,
wait_for_load: bool = True
) -> None
Scroll the page.
| PARAMETER | DESCRIPTION |
|---|---|
direction
|
The direction to scroll. Can be
TYPE:
|
height
|
The height to scroll. Default is the viewport height.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
back_forward_async
async
¶
Go back or forward in the browser history in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
direction
|
The direction to go. Can be
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if the direction is invalid. |
Source code in conatus/utils/browser/page.py
back_forward
¶
Go back or forward in the browser history.
| PARAMETER | DESCRIPTION |
|---|---|
direction
|
The direction to go. Can be
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
drag_async
async
¶
Drag the mouse.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The path to drag the mouse to. |
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
drag
¶
Drag the mouse.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The path to drag the mouse to. |
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
type_async
async
¶
Type text into the page in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The text to type.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
type
¶
Type text into the page.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The text to type.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
keypress_async
async
¶
Press keys into the page in an async context.
| PARAMETER | DESCRIPTION |
|---|---|
keys
|
The keys to press. |
duration
|
The duration to hold the key.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
keypress
¶
Press keys into the page.
| PARAMETER | DESCRIPTION |
|---|---|
keys
|
The keys to press. |
duration
|
The duration to hold the key.
TYPE:
|
wait_for_load
|
Whether to wait for the page to load. Defaults to
TYPE:
|
Source code in conatus/utils/browser/page.py
click_node_async
async
¶
click_node_async(node_id: int) -> None
Click on a node asynchronously.
| PARAMETER | DESCRIPTION |
|---|---|
node_id
|
The id of the node to click on.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
if the node id is not found in the input click nodes. |