Skip to content

Commit 9a7aea5

Browse files
committed
refactor: refactor screenshot path handling and enhance error checking
1 parent 8f2fd92 commit 9a7aea5

File tree

18 files changed

+44
-29
lines changed

18 files changed

+44
-29
lines changed

pydoll/browser/tab.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ async def refresh(
385385

386386
async def take_screenshot(
387387
self,
388-
path: str,
388+
path: Optional[str] = None,
389389
quality: int = 100,
390390
as_base64: bool = False,
391391
) -> Optional[str]:
@@ -402,8 +402,12 @@ async def take_screenshot(
402402
403403
Raises:
404404
InvalidFileExtension: If file extension not supported.
405+
ValueError: If path is None and as_base64 is False.
405406
"""
406-
output_extension = path.split('.')[-1]
407+
if not path and not as_base64:
408+
raise ValueError('path is required when as_base64 is False')
409+
410+
output_extension = path.split('.')[-1] if path else ScreenshotFormat.PNG
407411
if not ScreenshotFormat.has_value(output_extension):
408412
raise InvalidFileExtension(f'{output_extension} extension is not supported.')
409413

@@ -417,9 +421,10 @@ async def take_screenshot(
417421
if as_base64:
418422
return screenshot_data
419423

420-
screenshot_bytes = decode_base64_to_bytes(screenshot_data)
421-
async with aiofiles.open(path, 'wb') as file:
422-
await file.write(screenshot_bytes)
424+
if path:
425+
screenshot_bytes = decode_base64_to_bytes(screenshot_data)
426+
async with aiofiles.open(path, 'wb') as file:
427+
await file.write(screenshot_bytes)
423428

424429
return None
425430

@@ -540,13 +545,15 @@ async def expect_file_chooser(
540545
Args:
541546
files: File path(s) for upload.
542547
"""
548+
543549
async def event_handler(event):
544550
await self._execute_command(
545551
DomCommands.upload_files(
546552
files=files,
547553
backend_node_id=event['params']['backendNodeId'],
548554
)
549555
)
556+
550557
if self.page_events_enabled is False:
551558
_before_page_events_enabled = False
552559
await self.enable_page_events()

pydoll/commands/fetch_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def continue_request( # noqa: PLR0913, PLR0917
6969
post_data (Optional[dict]): The body data to send with the fetch
7070
request. Defaults to None.
7171
headers (Optional[list[HeaderEntry]]): A list of HTTP headers to include
72-
in the fetch request. Defaults to None.
72+
in the fetch request. Defaults to None.
7373
intercept_response (Optional[bool]): Indicates if the response
74-
should be intercepted. Defaults to None.
74+
should be intercepted. Defaults to None.
7575
7676
Returns:
7777
Command[Response]: A command for continuing the fetch request.

pydoll/commands/input_commands.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def dispatch_key_event( # noqa: PLR0913, PLR0917, PLR0912
9797
Not needed for 'keyUp' and 'rawKeyDown' events (default: "").
9898
unmodified_text: Text that would have been generated by the keyboard without modifiers
9999
(except for shift). Useful for shortcut key handling (default: "").
100-
keyIdentifier: Unique key identifier (e.g., 'U+0041') (default: "").
100+
key_identifier: Unique key identifier (e.g., 'U+0041') (default: "").
101101
code: Unique DOM defined string value for each physical key (e.g., 'KeyA')
102102
(default: "").
103103
key: Unique DOM defined string value describing the meaning of the key in the
@@ -180,30 +180,30 @@ def dispatch_mouse_event( # noqa: PLR0913, PLR0917
180180
- mouseWheel: Mouse wheel rotated
181181
x: X coordinate of the event relative to the main frame's viewport in CSS pixels.
182182
y: Y coordinate of the event relative to the main frame's viewport in CSS pixels.
183-
0 refers to the top of the viewport, and Y increases going down.
183+
0 refers to the top of the viewport, and Y increases going down.
184184
modifiers: Bit field representing pressed modifier keys. Values:
185-
Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
185+
Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
186186
timestamp: Time at which the event occurred, in seconds since epoch.
187187
button: Mouse button being pressed/released. Default is "none".
188-
Allowed values: "none", "left", "middle", "right", "back", "forward".
188+
Allowed values: "none", "left", "middle", "right", "back", "forward".
189189
click_count: Number of times the mouse button was clicked (default: 0).
190-
For example, 2 for a double-click.
190+
For example, 2 for a double-click.
191191
force: The normalized pressure, which has a range of [0,1] (default: 0).
192-
Used primarily for pressure-sensitive inputs.
192+
Used primarily for pressure-sensitive inputs.
193193
tangential_pressure: The normalized tangential pressure, which has a range
194-
of [-1,1] (default: 0). Used for stylus input.
194+
of [-1,1] (default: 0). Used for stylus input.
195195
tilt_x: The plane angle between the Y-Z plane and the plane containing both the stylus
196-
axis and the Y axis, in degrees of the range [-90,90]. A positive tiltX is
197-
to the right (default: 0).
196+
axis and the Y axis, in degrees of the range [-90,90]. A positive tiltX is
197+
to the right (default: 0).
198198
tilt_y: The plane angle between the X-Z plane and the plane containing both the stylus
199-
axis and the X axis, in degrees of the range [-90,90]. A positive tiltY is
200-
towards the user (default: 0).
199+
axis and the X axis, in degrees of the range [-90,90]. A positive tiltY is
200+
towards the user (default: 0).
201201
twist: The clockwise rotation of a pen stylus around its own major axis,
202-
in degrees in the range [0,359] (default: 0).
202+
in degrees in the range [0,359] (default: 0).
203203
delta_x: X delta in CSS pixels for mouse wheel event (default: 0).
204-
Positive values scroll right.
204+
Positive values scroll right.
205205
delta_y: Y delta in CSS pixels for mouse wheel event (default: 0).
206-
Positive values scroll up.
206+
Positive values scroll up.
207207
pointer_type: Pointer type (default: "mouse"). Allowed values: "mouse", "pen".
208208
209209
Returns:
@@ -317,7 +317,7 @@ def dispatch_drag_event(
317317
- dragCancel: Fired when a drag operation is being canceled
318318
x: X coordinate of the event relative to the main frame's viewport in CSS pixels.
319319
y: Y coordinate of the event relative to the main frame's viewport in CSS pixels.
320-
0 refers to the top of the viewport, and Y increases going down.
320+
0 refers to the top of the viewport, and Y increases going down.
321321
data: Drag data containing items being dragged, their MIME types, and other information.
322322
modifiers: Bit field representing pressed modifier keys. Values:
323323
Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).

pydoll/commands/network_commands.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ def clear_browser_cookies() -> Command[Response]:
104104
to simulate a fresh user session without any previously stored
105105
cookies that might affect the application's behavior.
106106
107-
Args:
108-
None
109-
110107
Returns:
111108
Command[Response]: A command to clear all cookies in the browser.
112109
"""

pydoll/elements/__init__.py

Whitespace-only changes.

pydoll/elements/mixins/find_elements_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def find( # noqa: PLR0913, PLR0917
5555
timeout: int = 0,
5656
find_all: bool = False,
5757
raise_exc: bool = True,
58-
**attributes,
58+
**attributes: dict[str, str],
5959
) -> Union['WebElement', list['WebElement'], None]:
6060
"""
6161
Find element(s) using combination of common HTML attributes.

pydoll/elements/web_element.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async def get_bounds_using_js(self) -> dict[str, int]:
129129
response = await self._execute_script(Scripts.BOUNDS, return_by_value=True)
130130
return json.loads(response['result']['result']['value'])
131131

132-
async def get_screenshot(self, path: str, quality: int = 100):
132+
async def take_screenshot(self, path: str, quality: int = 100):
133133
"""
134134
Capture screenshot of this element only.
135135

pydoll/protocol/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Chrome DevTools Protocol implementation."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Browser domain implementation."""

pydoll/protocol/dom/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""DOM domain implementation."""

0 commit comments

Comments
 (0)