@@ -319,7 +319,15 @@ def has_cf(text):
319319 return False
320320
321321
322- def uc_special_open_if_cf (driver , url , proxy_string = None ):
322+ def uc_special_open_if_cf (
323+ driver ,
324+ url ,
325+ proxy_string = None ,
326+ mobile_emulator = None ,
327+ device_width = None ,
328+ device_height = None ,
329+ device_pixel_ratio = None ,
330+ ):
323331 if (
324332 url .startswith ("http:" ) or url .startswith ("https:" )
325333 ):
@@ -345,6 +353,36 @@ def uc_special_open_if_cf(driver, url, proxy_string=None):
345353 driver .close ()
346354 driver .switch_to .window (driver .window_handles [- 1 ])
347355 time .sleep (0.02 )
356+ if mobile_emulator :
357+ uc_metrics = {}
358+ if (
359+ type (device_width ) is int
360+ and type (device_height ) is int
361+ and type (device_pixel_ratio ) is int
362+ ):
363+ uc_metrics ["width" ] = device_width
364+ uc_metrics ["height" ] = device_height
365+ uc_metrics ["pixelRatio" ] = device_pixel_ratio
366+ else :
367+ uc_metrics ["width" ] = constants .Mobile .WIDTH
368+ uc_metrics ["height" ] = constants .Mobile .HEIGHT
369+ uc_metrics ["pixelRatio" ] = constants .Mobile .RATIO
370+ set_device_metrics_override = dict (
371+ {
372+ "width" : uc_metrics ["width" ],
373+ "height" : uc_metrics ["height" ],
374+ "deviceScaleFactor" : uc_metrics ["pixelRatio" ],
375+ "mobile" : True
376+ }
377+ )
378+ try :
379+ driver .execute_cdp_cmd (
380+ 'Emulation.setDeviceMetricsOverride' ,
381+ set_device_metrics_override
382+ )
383+ except Exception :
384+ pass
385+ time .sleep (0.03 )
348386 else :
349387 driver .default_get (url ) # The original one
350388 else :
@@ -739,7 +777,7 @@ def _set_chrome_options(
739777 "excludeSwitches" ,
740778 ["enable-automation" , "enable-logging" , "enable-blink-features" ],
741779 )
742- if mobile_emulator :
780+ if mobile_emulator and not is_using_uc ( undetectable , browser_name ) :
743781 emulator_settings = {}
744782 device_metrics = {}
745783 if (
@@ -751,9 +789,9 @@ def _set_chrome_options(
751789 device_metrics ["height" ] = device_height
752790 device_metrics ["pixelRatio" ] = device_pixel_ratio
753791 else :
754- device_metrics ["width" ] = 360
755- device_metrics ["height" ] = 640
756- device_metrics ["pixelRatio" ] = 2
792+ device_metrics ["width" ] = constants . Mobile . WIDTH
793+ device_metrics ["height" ] = constants . Mobile . HEIGHT
794+ device_metrics ["pixelRatio" ] = constants . Mobile . RATIO
757795 emulator_settings ["deviceMetrics" ] = device_metrics
758796 if user_agent :
759797 emulator_settings ["userAgent" ] = user_agent
@@ -1279,8 +1317,8 @@ def get_driver(
12791317 if (uc_cdp_events or uc_subprocess ) and not undetectable :
12801318 undetectable = True
12811319 if is_using_uc (undetectable , browser_name ) and mobile_emulator :
1282- mobile_emulator = False
1283- user_agent = None
1320+ if not user_agent :
1321+ user_agent = constants . Mobile . AGENT
12841322 if page_load_strategy and page_load_strategy .lower () == "none" :
12851323 settings .PAGE_LOAD_STRATEGY = "none"
12861324 proxy_auth = False
@@ -2359,7 +2397,7 @@ def get_local_driver(
23592397 elif headless :
23602398 if "--headless" not in edge_options .arguments :
23612399 edge_options .add_argument ("--headless" )
2362- if mobile_emulator :
2400+ if mobile_emulator and not is_using_uc ( undetectable , browser_name ) :
23632401 emulator_settings = {}
23642402 device_metrics = {}
23652403 if (
@@ -2371,9 +2409,9 @@ def get_local_driver(
23712409 device_metrics ["height" ] = device_height
23722410 device_metrics ["pixelRatio" ] = device_pixel_ratio
23732411 else :
2374- device_metrics ["width" ] = 360
2375- device_metrics ["height" ] = 640
2376- device_metrics ["pixelRatio" ] = 2
2412+ device_metrics ["width" ] = constants . Mobile . WIDTH
2413+ device_metrics ["height" ] = constants . Mobile . HEIGHT
2414+ device_metrics ["pixelRatio" ] = constants . Mobile . RATIO
23772415 emulator_settings ["deviceMetrics" ] = device_metrics
23782416 if user_agent :
23792417 emulator_settings ["userAgent" ] = user_agent
@@ -3416,7 +3454,13 @@ def get_local_driver(
34163454 driver .default_get = driver .get # Save copy of original
34173455 if uc_activated :
34183456 driver .get = lambda url : uc_special_open_if_cf (
3419- driver , url , proxy_string
3457+ driver ,
3458+ url ,
3459+ proxy_string ,
3460+ mobile_emulator ,
3461+ device_width ,
3462+ device_height ,
3463+ device_pixel_ratio ,
34203464 )
34213465 driver .uc_open = lambda url : uc_open (driver , url )
34223466 driver .uc_open_with_tab = (
@@ -3425,6 +3469,35 @@ def get_local_driver(
34253469 driver .uc_open_with_reconnect = (
34263470 lambda url : uc_open_with_reconnect (driver , url )
34273471 )
3472+ if mobile_emulator :
3473+ uc_metrics = {}
3474+ if (
3475+ type (device_width ) is int
3476+ and type (device_height ) is int
3477+ and type (device_pixel_ratio ) is int
3478+ ):
3479+ uc_metrics ["width" ] = device_width
3480+ uc_metrics ["height" ] = device_height
3481+ uc_metrics ["pixelRatio" ] = device_pixel_ratio
3482+ else :
3483+ uc_metrics ["width" ] = constants .Mobile .WIDTH
3484+ uc_metrics ["height" ] = constants .Mobile .HEIGHT
3485+ uc_metrics ["pixelRatio" ] = constants .Mobile .RATIO
3486+ set_device_metrics_override = dict (
3487+ {
3488+ "width" : uc_metrics ["width" ],
3489+ "height" : uc_metrics ["height" ],
3490+ "deviceScaleFactor" : uc_metrics ["pixelRatio" ],
3491+ "mobile" : True
3492+ }
3493+ )
3494+ try :
3495+ driver .execute_cdp_cmd (
3496+ 'Emulation.setDeviceMetricsOverride' ,
3497+ set_device_metrics_override
3498+ )
3499+ except Exception :
3500+ pass
34283501 return extend_driver (driver )
34293502 else : # Running headless on Linux (and not using --uc)
34303503 try :
0 commit comments