Skip to content

Commit c770fa8

Browse files
committed
Add command-line shortcuts for Pure CDP Mode (sb_cdp)
1 parent b9a9ff2 commit c770fa8

File tree

1 file changed

+210
-4
lines changed

1 file changed

+210
-4
lines changed

seleniumbase/undetected/cdp_driver/cdp_util.py

Lines changed: 210 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ async def start(
258258
config: Optional[Config] = None,
259259
*,
260260
user_data_dir: Optional[PathLike] = None,
261-
headless: Optional[bool] = False,
262-
incognito: Optional[bool] = False,
263-
guest: Optional[bool] = False,
261+
headless: Optional[bool] = None,
262+
incognito: Optional[bool] = None,
263+
guest: Optional[bool] = None,
264264
browser_executable_path: Optional[PathLike] = None,
265265
browser_args: Optional[List[str]] = None,
266266
xvfb_metrics: Optional[List[str]] = None, # "Width,Height" for Linux
267-
ad_block: Optional[bool] = False,
267+
ad_block: Optional[bool] = None,
268268
sandbox: Optional[bool] = True,
269269
lang: Optional[str] = None, # Set the Language Locale Code
270270
host: Optional[str] = None, # Chrome remote-debugging-host
@@ -318,6 +318,210 @@ async def start(
318318
(For example, ensuring shadow-root is always in "open" mode.)
319319
:type expert: bool
320320
"""
321+
sys_argv = sys.argv
322+
arg_join = " ".join(sys_argv)
323+
if headless is None:
324+
if "--headless" in sys_argv:
325+
headless = True
326+
else:
327+
headless = False
328+
if headed is None:
329+
if "--gui" in sys_argv or "--headed" in sys_argv:
330+
headed = True
331+
else:
332+
headed = False
333+
if xvfb is None:
334+
if "--xvfb" in sys_argv:
335+
xvfb = True
336+
else:
337+
xvfb = False
338+
if incognito is None:
339+
if "--incognito" in sys_argv:
340+
incognito = True
341+
else:
342+
incognito = False
343+
if guest is None:
344+
if "--guest" in sys_argv:
345+
guest = True
346+
else:
347+
guest = False
348+
if ad_block is None:
349+
if "--ad-block" in sys_argv or "--ad_block" in sys_argv:
350+
ad_block = True
351+
else:
352+
ad_block = False
353+
if xvfb_metrics is None and "--xvfb-metrics" in arg_join:
354+
x_m = xvfb_metrics
355+
count = 0
356+
for arg in sys_argv:
357+
if arg.startswith("--xvfb-metrics="):
358+
x_m = arg.split("--xvfb-metrics=")[1]
359+
break
360+
elif arg == "--xvfb-metrics" and len(sys_argv) > count + 1:
361+
x_m = sys_argv[count + 1]
362+
if x_m.startswith("-"):
363+
x_m = None
364+
break
365+
count += 1
366+
if x_m:
367+
if x_m.startswith('"') and x_m.endswith('"'):
368+
x_m = x_m[1:-1]
369+
elif x_m.startswith("'") and x_m.endswith("'"):
370+
x_m = x_m[1:-1]
371+
xvfb_metrics = x_m
372+
if agent is None and "user_agent" not in kwargs and "--agent" in arg_join:
373+
count = 0
374+
for arg in sys_argv:
375+
if arg.startswith("--agent="):
376+
agent = arg.split("--agent=")[1]
377+
break
378+
elif arg == "--agent" and len(sys_argv) > count + 1:
379+
agent = sys_argv[count + 1]
380+
if agent.startswith("-"):
381+
agent = None
382+
break
383+
count += 1
384+
if agent:
385+
if agent.startswith('"') and agent.endswith('"'):
386+
agent = agent[1:-1]
387+
elif agent.startswith("'") and agent.endswith("'"):
388+
agent = agent[1:-1]
389+
if (
390+
geoloc is None
391+
and "geolocation" not in kwargs
392+
and "--geolocation" in arg_join
393+
):
394+
count = 0
395+
for arg in sys_argv:
396+
if arg.startswith("--geolocation="):
397+
geoloc = arg.split("--geolocation=")[1]
398+
break
399+
elif arg == "--geolocation" and len(sys_argv) > count + 1:
400+
geoloc = sys_argv[count + 1]
401+
if geoloc.startswith("-"):
402+
geoloc = None
403+
break
404+
count += 1
405+
if geoloc:
406+
if geoloc.startswith('"') and geoloc.endswith('"'):
407+
geoloc = geoloc[1:-1]
408+
elif geoloc.startswith("'") and geoloc.endswith("'"):
409+
geoloc = geoloc[1:-1]
410+
if geoloc:
411+
import ast
412+
geoloc = ast.literal_eval(geoloc)
413+
if not lang and "locale" not in kwargs and "locale_code" not in kwargs:
414+
if "--locale" in arg_join:
415+
count = 0
416+
for arg in sys_argv:
417+
if arg.startswith("--locale="):
418+
lang = arg.split("--locale=")[1]
419+
break
420+
elif arg == "--locale" and len(sys_argv) > count + 1:
421+
lang = sys_argv[count + 1]
422+
if lang.startswith("-"):
423+
lang = None
424+
break
425+
count += 1
426+
elif "--lang" in arg_join:
427+
count = 0
428+
for arg in sys_argv:
429+
if arg.startswith("--lang="):
430+
lang = arg.split("--lang=")[1]
431+
break
432+
elif arg == "--lang" and len(sys_argv) > count + 1:
433+
lang = sys_argv[count + 1]
434+
if lang.startswith("-"):
435+
lang = None
436+
break
437+
count += 1
438+
if lang:
439+
if lang.startswith('"') and lang.endswith('"'):
440+
lang = lang[1:-1]
441+
elif lang.startswith("'") and lang.endswith("'"):
442+
lang = lang[1:-1]
443+
if not browser_executable_path and "binary_location" not in kwargs:
444+
bin_loc = None
445+
if "--binary-location" in arg_join or "--binary_location" in arg_join:
446+
bin_loc_cmd = "--binary-location"
447+
if "--binary_location" in arg_join:
448+
bin_loc_cmd = "--binary_location"
449+
count = 0
450+
bin_loc = None
451+
for arg in sys_argv:
452+
if arg.startswith("%s=" % bin_loc_cmd):
453+
bin_loc = arg.split("%s=" % bin_loc_cmd)[1]
454+
break
455+
elif arg == bin_loc_cmd and len(sys_argv) > count + 1:
456+
bin_loc = sys_argv[count + 1]
457+
if bin_loc.startswith("-"):
458+
bin_loc = None
459+
break
460+
count += 1
461+
elif "--bl=" in arg_join:
462+
count = 0
463+
bin_loc = None
464+
for arg in sys_argv:
465+
if arg.startswith("--bl="):
466+
bin_loc = arg.split("--bl=")[1]
467+
break
468+
count += 1
469+
if bin_loc:
470+
if bin_loc.startswith('"') and bin_loc.endswith('"'):
471+
bin_loc = bin_loc[1:-1]
472+
elif bin_loc.startswith("'") and bin_loc.endswith("'"):
473+
bin_loc = bin_loc[1:-1]
474+
if bin_loc and not os.path.exists(bin_loc):
475+
print(" No browser executable at PATH {%s}! " % bin_loc)
476+
print(" Using default Chrome browser instead!")
477+
bin_loc = None
478+
browser_executable_path = bin_loc
479+
if proxy is None and "--proxy" in arg_join:
480+
proxy_string = None
481+
if "--proxy=" in arg_join:
482+
proxy_string = arg_join.split("--proxy=")[1].split(" ")[0]
483+
elif "--proxy " in arg_join:
484+
proxy_string = arg_join.split("--proxy ")[1].split(" ")[0]
485+
if proxy_string:
486+
if proxy_string.startswith('"') and proxy_string.endswith('"'):
487+
proxy_string = proxy_string[1:-1]
488+
elif proxy_string.startswith("'") and proxy_string.endswith("'"):
489+
proxy_string = proxy_string[1:-1]
490+
proxy = proxy_string
491+
if tzone is None and "timezone" not in kwargs and "--timezone" in arg_join:
492+
tz_string = None
493+
if "--timezone=" in arg_join:
494+
tz_string = arg_join.split("--timezone=")[1].split(" ")[0]
495+
elif "--timezone " in arg_join:
496+
tz_string = arg_join.split("--timezone ")[1].split(" ")[0]
497+
if tz_string:
498+
if tz_string.startswith('"') and tz_string.endswith('"'):
499+
tz_string = proxy_string[1:-1]
500+
elif tz_string.startswith("'") and tz_string.endswith("'"):
501+
tz_string = proxy_string[1:-1]
502+
tzone = tz_string
503+
platform_var = None
504+
if (
505+
"platform" not in kwargs
506+
and "plat" not in kwargs
507+
and "--platform" in arg_join
508+
):
509+
count = 0
510+
for arg in sys_argv:
511+
if arg.startswith("--platform="):
512+
platform_var = arg.split("--platform=")[1]
513+
break
514+
elif arg == "--platform" and len(sys_argv) > count + 1:
515+
platform_var = sys_argv[count + 1]
516+
if platform_var.startswith("-"):
517+
platform_var = None
518+
break
519+
count += 1
520+
if platform_var:
521+
if platform_var.startswith('"') and platform_var.endswith('"'):
522+
platform_var = platform_var[1:-1]
523+
elif platform_var.startswith("'") and platform_var.endswith("'"):
524+
platform_var = platform_var[1:-1]
321525
if IS_LINUX and not headless and not headed and not xvfb:
322526
xvfb = True # The default setting on Linux
323527
__activate_virtual_display_as_needed(headless, headed, xvfb, xvfb_metrics)
@@ -404,6 +608,8 @@ async def start(
404608
sb_config._cdp_platform = kwargs["platform"]
405609
elif "plat" in kwargs:
406610
sb_config._cdp_platform = kwargs["plat"]
611+
elif platform_var:
612+
sb_config._cdp_platform = platform_var
407613
else:
408614
sb_config._cdp_platform = None
409615
return driver

0 commit comments

Comments
 (0)