@@ -319,7 +319,15 @@ def has_cf(text):
319
319
return False
320
320
321
321
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
+ ):
323
331
if (
324
332
url .startswith ("http:" ) or url .startswith ("https:" )
325
333
):
@@ -345,6 +353,36 @@ def uc_special_open_if_cf(driver, url, proxy_string=None):
345
353
driver .close ()
346
354
driver .switch_to .window (driver .window_handles [- 1 ])
347
355
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 )
348
386
else :
349
387
driver .default_get (url ) # The original one
350
388
else :
@@ -739,7 +777,7 @@ def _set_chrome_options(
739
777
"excludeSwitches" ,
740
778
["enable-automation" , "enable-logging" , "enable-blink-features" ],
741
779
)
742
- if mobile_emulator :
780
+ if mobile_emulator and not is_using_uc ( undetectable , browser_name ) :
743
781
emulator_settings = {}
744
782
device_metrics = {}
745
783
if (
@@ -751,9 +789,9 @@ def _set_chrome_options(
751
789
device_metrics ["height" ] = device_height
752
790
device_metrics ["pixelRatio" ] = device_pixel_ratio
753
791
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
757
795
emulator_settings ["deviceMetrics" ] = device_metrics
758
796
if user_agent :
759
797
emulator_settings ["userAgent" ] = user_agent
@@ -1279,8 +1317,8 @@ def get_driver(
1279
1317
if (uc_cdp_events or uc_subprocess ) and not undetectable :
1280
1318
undetectable = True
1281
1319
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
1284
1322
if page_load_strategy and page_load_strategy .lower () == "none" :
1285
1323
settings .PAGE_LOAD_STRATEGY = "none"
1286
1324
proxy_auth = False
@@ -2359,7 +2397,7 @@ def get_local_driver(
2359
2397
elif headless :
2360
2398
if "--headless" not in edge_options .arguments :
2361
2399
edge_options .add_argument ("--headless" )
2362
- if mobile_emulator :
2400
+ if mobile_emulator and not is_using_uc ( undetectable , browser_name ) :
2363
2401
emulator_settings = {}
2364
2402
device_metrics = {}
2365
2403
if (
@@ -2371,9 +2409,9 @@ def get_local_driver(
2371
2409
device_metrics ["height" ] = device_height
2372
2410
device_metrics ["pixelRatio" ] = device_pixel_ratio
2373
2411
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
2377
2415
emulator_settings ["deviceMetrics" ] = device_metrics
2378
2416
if user_agent :
2379
2417
emulator_settings ["userAgent" ] = user_agent
@@ -3416,7 +3454,13 @@ def get_local_driver(
3416
3454
driver .default_get = driver .get # Save copy of original
3417
3455
if uc_activated :
3418
3456
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 ,
3420
3464
)
3421
3465
driver .uc_open = lambda url : uc_open (driver , url )
3422
3466
driver .uc_open_with_tab = (
@@ -3425,6 +3469,35 @@ def get_local_driver(
3425
3469
driver .uc_open_with_reconnect = (
3426
3470
lambda url : uc_open_with_reconnect (driver , url )
3427
3471
)
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
3428
3501
return extend_driver (driver )
3429
3502
else : # Running headless on Linux (and not using --uc)
3430
3503
try :
0 commit comments