Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions playwright/_impl/_element_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import base64
import mimetypes
from pathlib import Path
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -323,6 +324,8 @@ async def screenshot(
) -> bytes:
params = locals_to_params(locals())
if "path" in params:
if "type" not in params:
params["type"] = determine_screenshot_type(params["path"])
del params["path"]
if "mask" in params:
params["mask"] = list(
Expand Down Expand Up @@ -450,3 +453,12 @@ def convert_select_option_values(
elements = list(map(lambda e: e._channel, element))

return dict(options=options, elements=elements)


def determine_screenshot_type(path: Union[str, Path]) -> Literal["jpeg", "png"]:
mime_type, _ = mimetypes.guess_type(path)
if mime_type == "image/png":
return "png"
if mime_type == "image/jpeg":
return "jpeg"
raise Error(f'Unsupported screenshot mime type for path "{path}": {mime_type}')
4 changes: 3 additions & 1 deletion playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
)
from playwright._impl._console_message import ConsoleMessage
from playwright._impl._download import Download
from playwright._impl._element_handle import ElementHandle
from playwright._impl._element_handle import ElementHandle, determine_screenshot_type
from playwright._impl._errors import Error, TargetClosedError, is_target_closed_error
from playwright._impl._event_context_manager import EventContextManagerImpl
from playwright._impl._file_chooser import FileChooser
Expand Down Expand Up @@ -800,6 +800,8 @@ async def screenshot(
) -> bytes:
params = locals_to_params(locals())
if "path" in params:
if "type" not in params:
params["type"] = determine_screenshot_type(params["path"])
del params["path"]
if "mask" in params:
params["mask"] = list(
Expand Down
Loading