File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed
Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change 1313# limitations under the License.
1414
1515import base64
16+ import mimetypes
1617from pathlib import Path
1718from 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 } ' )
Original file line number Diff line number Diff line change 5151)
5252from playwright ._impl ._console_message import ConsoleMessage
5353from playwright ._impl ._download import Download
54- from playwright ._impl ._element_handle import ElementHandle
54+ from playwright ._impl ._element_handle import ElementHandle , determine_screenshot_type
5555from playwright ._impl ._errors import Error , TargetClosedError , is_target_closed_error
5656from playwright ._impl ._event_context_manager import EventContextManagerImpl
5757from 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 (
You can’t perform that action at this time.
0 commit comments