1212from playwright .sync_api import BrowserContext , Page , Playwright , sync_playwright
1313
1414from sentience ._extension_loader import find_extension_path
15- from sentience .models import ProxyConfig , StorageState
15+ from sentience .models import ProxyConfig , StorageState , Viewport
1616
1717# Import stealth for bot evasion (optional - graceful fallback if not available)
1818try :
@@ -36,7 +36,7 @@ def __init__(
3636 storage_state : str | Path | StorageState | dict | None = None ,
3737 record_video_dir : str | Path | None = None ,
3838 record_video_size : dict [str , int ] | None = None ,
39- viewport : dict [str , int ] | None = None ,
39+ viewport : Viewport | dict [str , int ] | None = None ,
4040 ):
4141 """
4242 Initialize Sentience browser
@@ -69,11 +69,11 @@ def __init__(
6969 Examples: {"width": 1280, "height": 800} (default)
7070 {"width": 1920, "height": 1080} (1080p)
7171 If None, defaults to 1280x800.
72- viewport: Optional viewport size as dict with 'width' and 'height' keys.
73- Examples: {" width": 1280, " height": 800} (default)
74- {" width": 1920, " height": 1080} (Full HD)
75- {"width": 375 , "height": 667 } (iPhone )
76- If None, defaults to 1280x800 .
72+ viewport: Optional viewport size as Viewport object or dict with 'width' and 'height' keys.
73+ Examples: Viewport( width= 1280, height= 800) (default)
74+ Viewport( width= 1920, height= 1080) (Full HD)
75+ {"width": 1280 , "height": 800 } (dict also supported )
76+ If None, defaults to Viewport(width=1280, height=800) .
7777 """
7878 self .api_key = api_key
7979 # Only set api_url if api_key is provided, otherwise None (free tier)
@@ -101,8 +101,13 @@ def __init__(
101101 self .record_video_dir = record_video_dir
102102 self .record_video_size = record_video_size or {"width" : 1280 , "height" : 800 }
103103
104- # Viewport configuration
105- self .viewport = viewport or {"width" : 1280 , "height" : 800 }
104+ # Viewport configuration - convert dict to Viewport if needed
105+ if viewport is None :
106+ self .viewport = Viewport (width = 1280 , height = 800 )
107+ elif isinstance (viewport , dict ):
108+ self .viewport = Viewport (width = viewport ["width" ], height = viewport ["height" ])
109+ else :
110+ self .viewport = viewport
106111
107112 self .playwright : Playwright | None = None
108113 self .context : BrowserContext | None = None
@@ -201,7 +206,7 @@ def start(self) -> None:
201206 "user_data_dir" : user_data_dir ,
202207 "headless" : False , # IMPORTANT: See note above
203208 "args" : args ,
204- "viewport" : self .viewport ,
209+ "viewport" : { "width" : self .viewport . width , "height" : self . viewport . height } ,
205210 # Remove "HeadlessChrome" from User Agent automatically
206211 "user_agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36" ,
207212 }
0 commit comments