Skip to content

Commit 789ab2a

Browse files
committed
fix: screenshot type inferred from path file extension
1 parent 1be34f2 commit 789ab2a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

playwright/_impl/_element_handle.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import base64
16+
import mimetypes
1617
from pathlib import Path
1718
from typing import (
1819
TYPE_CHECKING,
@@ -323,6 +324,8 @@ async def screenshot(
323324
) -> bytes:
324325
params = locals_to_params(locals())
325326
if "path" in params:
327+
if "type" not in params:
328+
params["type"] = determine_screenshot_type(params["path"])
326329
del params["path"]
327330
if "mask" in params:
328331
params["mask"] = list(
@@ -450,3 +453,12 @@ def convert_select_option_values(
450453
elements = list(map(lambda e: e._channel, element))
451454

452455
return dict(options=options, elements=elements)
456+
457+
458+
def determine_screenshot_type(path: Union[str, Path]) -> Literal["jpeg", "png"]:
459+
mime_type, _ = mimetypes.guess_type(path)
460+
if mime_type == "image/png":
461+
return "png"
462+
if mime_type == "image/jpeg":
463+
return "jpeg"
464+
raise Error(f'Unsupported screenshot mime type for path "{path}": {mime_type}')

playwright/_impl/_page.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
)
5252
from playwright._impl._console_message import ConsoleMessage
5353
from playwright._impl._download import Download
54-
from playwright._impl._element_handle import ElementHandle
54+
from playwright._impl._element_handle import ElementHandle, determine_screenshot_type
5555
from playwright._impl._errors import Error, TargetClosedError, is_target_closed_error
5656
from playwright._impl._event_context_manager import EventContextManagerImpl
5757
from playwright._impl._file_chooser import FileChooser
@@ -800,6 +800,8 @@ async def screenshot(
800800
) -> bytes:
801801
params = locals_to_params(locals())
802802
if "path" in params:
803+
if "type" not in params:
804+
params["type"] = determine_screenshot_type(params["path"])
803805
del params["path"]
804806
if "mask" in params:
805807
params["mask"] = list(

0 commit comments

Comments
 (0)