-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbrowser.py
More file actions
1214 lines (1151 loc) · 44.2 KB
/
Copy pathbrowser.py
File metadata and controls
1214 lines (1151 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""CDP-Driver is based on NoDriver"""
from __future__ import annotations
import asyncio
import atexit
import fasteners
import http.cookiejar
import json
import logging
import os
import pathlib
import pickle
import psutil
import re
import shutil
import time
import urllib.parse
import urllib.request
import warnings
from collections import defaultdict
from contextlib import suppress
from seleniumbase import config as sb_config
from seleniumbase.fixtures import constants
from seleniumbase.fixtures import shared_utils
from typing import List, Optional, Set, Tuple, Union
import mycdp as cdp
from . import cdp_util as util
from . import tab
from ._contradict import ContraDict
from .config import PathLike, Config, is_posix
from .connection import Connection
logger = logging.getLogger(__name__)
def get_registered_instances():
return __registered__instances__
def deconstruct_browser():
for _ in __registered__instances__:
if not _.stopped:
_.stop(deconstruct=True)
max_attempts = 5
for attempt in range(max_attempts):
try:
if _.config and not _.config.uses_custom_data_dir:
if os.path.exists(_.config.user_data_dir):
shutil.rmtree(
_.config.user_data_dir, ignore_errors=False
)
if not os.path.exists(_.config.user_data_dir):
break
else:
time.sleep(0.12)
except FileNotFoundError:
break
except (PermissionError, OSError) as e:
if attempt == max_attempts - 1:
logger.debug(
"Problem removing data dir %s\n"
"Consider checking whether it's there "
"and remove it by hand\nerror: %s"
% (_.config.user_data_dir, e)
)
break
time.sleep(0.12)
continue
logging.debug("Temp profile %s was removed." % _.config.user_data_dir)
class Browser:
"""
The Browser object is the "root" of the hierarchy
and contains a reference to the browser parent process.
There should usually be only 1 instance of this.
All opened tabs, extra browser screens,
and resources will not cause a new Browser process,
but rather create additional :class:`Tab` objects.
So, besides starting your instance and first/additional tabs,
you don't actively use it a lot under normal conditions.
Tab objects will represent and control:
- tabs (as you know them)
- browser windows (new window)
- iframe
- background processes
Note:
The Browser object is not instantiated by __init__
but using the asynchronous :meth:`Browser.create` method.
Note:
In Chromium based browsers, there is a parent process which keeps
running all the time, even if there are no visible browser windows.
Sometimes it's stubborn to close it, so make sure that after using
this library, the browser is correctly and fully closed/exited/killed.
"""
_process: asyncio.subprocess.Process
_process_pid: int
_http: HTTPApi = None
_cookies: CookieJar = None
config: Config
connection: Connection
@classmethod
async def create(
cls,
config: Config = None,
*,
user_data_dir: PathLike = None,
headless: bool = False,
incognito: bool = False,
guest: bool = False,
browser_executable_path: PathLike = None,
browser_args: List[str] = None,
sandbox: bool = True,
host: str = None,
port: int = None,
**kwargs,
) -> Browser:
"""Entry point for creating an instance."""
if not config:
config = Config(
user_data_dir=user_data_dir,
headless=headless,
incognito=incognito,
guest=guest,
browser_executable_path=browser_executable_path,
browser_args=browser_args or [],
sandbox=sandbox,
host=host,
port=port,
**kwargs,
)
try:
instance = cls(config)
await instance.start()
except Exception:
time.sleep(0.15)
instance = cls(config)
await instance.start()
return instance
def __init__(self, config: Config, **kwargs):
"""
Constructor. To create a instance, use :py:meth:`Browser.create(...)`
:param config:
"""
try:
asyncio.get_running_loop()
except RuntimeError:
raise RuntimeError(
"{0} objects of this class are created "
"using await {0}.create()".format(
self.__class__.__name__
)
)
self.config = config
self.targets: List = []
self.info = None
self._target = None
self._process = None
self._process_pid = None
self._keep_user_data_dir = None
self._is_updating = asyncio.Event()
self.connection: Connection = None
logger.debug("Session object initialized: %s" % vars(self))
@property
def websocket_url(self):
return self.info.webSocketDebuggerUrl
@property
def main_tab(self) -> tab.Tab:
"""Returns the target which was launched with the browser."""
return sorted(
self.targets, key=lambda x: x.type_ == "page", reverse=True
)[0]
@property
def tabs(self) -> List[tab.Tab]:
"""Returns the current targets which are of type "page"."""
tabs = filter(lambda item: item.type_ == "page", self.targets)
return list(tabs)
@property
def cookies(self) -> CookieJar:
if not self._cookies:
self._cookies = CookieJar(self)
return self._cookies
@property
def stopped(self):
if self._process and self._process.returncode is None:
return False
return True
async def wait(self, time: Union[float, int] = 1) -> Browser:
"""Wait for <time> seconds. Important to use,
especially in between page navigation.
:param time:
"""
return await asyncio.sleep(time, result=self)
sleep = wait
"""Alias for wait"""
async def _handle_target_update(
self,
event: Union[
cdp.target.TargetInfoChanged,
cdp.target.TargetDestroyed,
cdp.target.TargetCreated,
cdp.target.TargetCrashed,
],
):
"""This is an internal handler which updates the targets
when Chrome emits the corresponding event."""
if isinstance(event, cdp.target.TargetInfoChanged):
target_info = event.target_info
current_tab = next(
filter(
lambda item: item.target_id == target_info.target_id,
self.targets,
)
)
current_target = current_tab.target
if logger.getEffectiveLevel() <= 10:
changes = util.compare_target_info(
current_target, target_info
)
changes_string = ""
for change in changes:
key, old, new = change
changes_string += f"\n{key}: {old} => {new}\n"
logger.debug(
"Target #%d has changed: %s"
% (self.targets.index(current_tab), changes_string)
)
current_tab.target = target_info
elif isinstance(event, cdp.target.TargetCreated):
target_info: cdp.target.TargetInfo = event.target_info
websocket_url = (
f"ws://{self.config.host}:{self.config.port}"
f"/devtools/{target_info.type_ or 'page'}"
f"/{target_info.target_id}"
)
async with tab.Tab(
websocket_url=websocket_url,
target=target_info,
browser=self
) as new_target:
self.targets.append(new_target)
logger.debug(
"Target #%d created => %s"
% (len(self.targets), new_target)
)
elif isinstance(event, cdp.target.TargetDestroyed):
current_tab = next(
filter(
lambda item: item.target_id == event.target_id,
self.targets,
)
)
logger.debug(
"Target removed. id # %d => %s"
% (self.targets.index(current_tab), current_tab)
)
self.targets.remove(current_tab)
def is_running(self):
"""Check if the browser is still running."""
if not getattr(self, "_process_pid", None):
return False
# Not good enough: return psutil.pid_exists(self._process_pid)
try:
process = psutil.Process(self._process_pid)
# Check for PID recycling: Is this actually our process?
if hasattr(self, "_process_create_time"):
if process.create_time() != self._process_create_time:
return False # The PID was reused by a different process!
# If the process is a zombie, assume the browser isn't running
return process.status() != psutil.STATUS_ZOMBIE
except psutil.NoSuchProcess:
return False
except Exception:
return None
def get_rd_host(self):
return self.config.host
def get_rd_port(self):
return self.config.port
def get_rd_url(self):
"""Returns the remote-debugging URL, which is used for
allowing the Playwright integration to launch stealthy.
Also sets an environment variable to hide this warning:
Deprecation: "url.parse() behavior is not standardized".
(github.com/microsoft/playwright-python/issues/3016)"""
os.environ["NODE_NO_WARNINGS"] = "1"
host = self.config.host
port = self.config.port
return f"http://{host}:{port}"
def get_endpoint_url(self):
return self.get_rd_url()
def get_port(self):
return self.get_rd_port()
async def set_auth(self, username, password, tab):
async def auth_challenge_handler(event: cdp.fetch.AuthRequired):
await tab.send(
cdp.fetch.continue_with_auth(
request_id=event.request_id,
auth_challenge_response=cdp.fetch.AuthChallengeResponse(
response="ProvideCredentials",
username=username,
password=password,
),
)
)
async def req_paused(event: cdp.fetch.RequestPaused):
await tab.send(
cdp.fetch.continue_request(request_id=event.request_id)
)
tab.add_handler(
cdp.fetch.RequestPaused,
lambda event: asyncio.create_task(req_paused(event)),
)
tab.add_handler(
cdp.fetch.AuthRequired,
lambda event: asyncio.create_task(auth_challenge_handler(event)),
)
await tab.send(cdp.fetch.enable(handle_auth_requests=True))
async def get(
self,
url="about:blank",
new_tab: bool = False,
new_window: bool = False,
**kwargs,
) -> tab.Tab:
"""Top level get. Utilizes the first tab to retrieve given url.
Convenience function known from selenium.
This function detects when DOM events have fired during navigation.
:param url: The URL to navigate to
:param new_tab: Open new tab
:param new_window: Open new window
:return: Page
"""
await asyncio.sleep(0.005)
if url and ":" not in url:
url = "https://" + url
if new_tab or new_window:
# Create new target using the browser session.
target_id = await self.connection.send(
cdp.target.create_target(
url, new_window=new_window, enable_begin_frame_control=True
)
)
connection: tab.Tab = next(
filter(
lambda item: (
item.type_ == "page" and item.target_id == target_id
),
self.targets,
)
)
connection.browser = self
else:
try:
# Most recently opened tab
connection = self.targets[-1]
await connection.sleep(0.005)
except Exception:
# First tab from browser.tabs
connection: tab.Tab = next(
filter(lambda item: item.type_ == "page", self.targets)
)
await connection.sleep(0.005)
_cdp_timezone = None
_cdp_user_agent = ""
_cdp_locale = None
_cdp_platform = None
_cdp_disable_csp = None
_cdp_geolocation = None
_cdp_mobile_mode = None
_cdp_recorder = None
_cdp_ad_block = None
if getattr(sb_config, "_cdp_timezone", None):
_cdp_timezone = sb_config._cdp_timezone
if getattr(sb_config, "_cdp_user_agent", None):
_cdp_user_agent = sb_config._cdp_user_agent
if getattr(sb_config, "_cdp_locale", None):
_cdp_locale = sb_config._cdp_locale
if getattr(sb_config, "_cdp_platform", None):
_cdp_platform = sb_config._cdp_platform
if getattr(sb_config, "_cdp_geolocation", None):
_cdp_geolocation = sb_config._cdp_geolocation
if getattr(sb_config, "_cdp_mobile_mode", None):
_cdp_mobile_mode = sb_config._cdp_mobile_mode
if getattr(sb_config, "ad_block_on", None):
_cdp_ad_block = sb_config.ad_block_on
if getattr(sb_config, "disable_csp", None):
_cdp_disable_csp = sb_config.disable_csp
if "timezone" in kwargs:
_cdp_timezone = kwargs["timezone"]
elif "tzone" in kwargs:
_cdp_timezone = kwargs["tzone"]
if "user_agent" in kwargs:
_cdp_user_agent = kwargs["user_agent"]
elif "agent" in kwargs:
_cdp_user_agent = kwargs["agent"]
if "locale" in kwargs:
_cdp_locale = kwargs["locale"]
elif "lang" in kwargs:
_cdp_locale = kwargs["lang"]
elif "locale_code" in kwargs:
_cdp_locale = kwargs["locale_code"]
if "platform" in kwargs:
_cdp_platform = kwargs["platform"]
elif "plat" in kwargs:
_cdp_platform = kwargs["plat"]
if "disable_csp" in kwargs:
_cdp_disable_csp = kwargs["disable_csp"]
if "geolocation" in kwargs:
_cdp_geolocation = kwargs["geolocation"]
elif "geoloc" in kwargs:
_cdp_geolocation = kwargs["geoloc"]
if "ad_block" in kwargs:
_cdp_ad_block = kwargs["ad_block"]
if "mobile" in kwargs:
_cdp_mobile_mode = kwargs["mobile"]
if "recorder" in kwargs:
_cdp_recorder = kwargs["recorder"]
headless = self.config.headless
await connection.sleep(0.01)
await connection.send(cdp.network.enable())
await connection.sleep(0.01)
if _cdp_timezone:
await connection.set_timezone(_cdp_timezone)
if _cdp_locale:
await connection.set_locale(_cdp_locale)
if _cdp_user_agent or _cdp_locale or _cdp_platform or headless:
if (
(headless and not _cdp_user_agent)
or (_cdp_user_agent and "Headless" in _cdp_user_agent)
):
agent = await self.main_tab.evaluate("navigator.userAgent")
if agent and "Headless" in agent:
_cdp_user_agent = agent.replace("Headless", "")
await connection.set_user_agent(
user_agent=_cdp_user_agent,
accept_language=_cdp_locale,
platform=_cdp_platform,
)
if _cdp_ad_block:
await connection.send(cdp.network.set_blocked_urls(
urls=[
"*.cloudflareinsights.com*",
"*.googlesyndication.com*",
"*.googletagmanager.com*",
"*.google-analytics.com*",
"*.amazon-adsystem.com*",
"*.adsafeprotected.com*",
"*.m.media-amazon.com*",
"*.ads.linkedin.com*",
"*.casalemedia.com*",
"*.doubleclick.net*",
"*.admanmedia.com*",
"*.quantserve.com*",
"*.ads.simpli.fi*",
"*.fastclick.net*",
"*.snigelweb.com*",
"*.bidswitch.net*",
"*.360yield.com*",
"*.adthrive.com*",
"*.pubmatic.com*",
"*.id5-sync.com*",
"*.moatads.com*",
"*.dotomi.com*",
"*.adsrvr.org*",
"*.atmtd.com*",
"*.liadm.com*",
"*.loopme.me*",
"*.adnxs.com*",
"*.openx.net*",
"*.tapad.com*",
"*.3lift.com*",
"*.turn.com*",
"*.2mdn.net*",
"*.cpx.to*",
"*.ad.gt*",
]
))
if _cdp_geolocation:
await connection.set_geolocation(_cdp_geolocation)
if _cdp_disable_csp:
await connection.send(cdp.page.set_bypass_csp(enabled=True))
if _cdp_mobile_mode:
await connection.send(
cdp.emulation.set_device_metrics_override(
width=412, height=732, device_scale_factor=3, mobile=True
)
)
# (The code below is for the Chrome 142 extension fix)
if (
getattr(sb_config, "_cdp_proxy", None)
and "@" in sb_config._cdp_proxy
and "auth" not in kwargs
):
username_and_password = sb_config._cdp_proxy.split("@")[0]
proxy_user = username_and_password.split(":")[0]
proxy_pass = username_and_password.split(":")[1]
if (
hasattr(self.main_tab, "_last_auth")
and self.main_tab._last_auth == username_and_password
):
pass # Auth was already set
else:
self.main_tab._last_auth = username_and_password
await self.set_auth(proxy_user, proxy_pass, self.tabs[0])
time.sleep(0.25)
if "auth" in kwargs and kwargs["auth"] and ":" in kwargs["auth"]:
username_and_password = kwargs["auth"]
proxy_user = username_and_password.split(":")[0]
proxy_pass = username_and_password.split(":")[1]
if (
hasattr(self.main_tab, "_last_auth")
and self.main_tab._last_auth == username_and_password
):
pass # Auth was already set
else:
self.main_tab._last_auth = username_and_password
await self.set_auth(proxy_user, proxy_pass, self.tabs[0])
time.sleep(0.25)
await connection.sleep(0.15)
frame_id, loader_id, *_ = await connection.send(
cdp.page.navigate(url)
)
major_browser_version = None
try:
major_browser_version = (
int(self.info["Browser"].split("/")[-1].split(".")[0])
)
except Exception:
pass
if (
_cdp_recorder
and (
not hasattr(sb_config, "browser")
or (
sb_config.browser == "chrome"
and (
not major_browser_version
or major_browser_version >= 142
)
)
)
):
# (The code below is for the Chrome 142 extension fix)
from seleniumbase.js_code.recorder_js import recorder_js
recorder_code = (
"""window.onload = function() { %s };""" % recorder_js
)
await connection.send(
cdp.page.add_script_to_evaluate_on_new_document(recorder_code)
)
await connection.sleep(0.25)
await self.wait(0.05)
await connection.send(cdp.runtime.evaluate(recorder_js))
# Update the frame_id on the tab
connection.frame_id = frame_id
connection.browser = self
# Give settings time to take effect
await connection.sleep(0.25)
await self.wait(0.05)
return connection
async def start(self=None) -> Browser:
"""Launches the actual browser."""
if not self:
warnings.warn(
"Use ``await Browser.create()`` to create a new instance!"
)
return
if self._process or self._process_pid:
if self._process.returncode is not None:
return await self.create(config=self.config)
warnings.warn(
"Ignored! This call has no effect when already running!"
)
return
# self.config.update(kwargs)
connect_existing = False
if self.config.host is not None and self.config.port is not None:
connect_existing = True
else:
self.config.host = "127.0.0.1"
self.config.port = util.free_port()
if not connect_existing:
logger.debug(
"BROWSER EXECUTABLE PATH: %s"
% self.config.browser_executable_path,
)
if not pathlib.Path(self.config.browser_executable_path).exists():
raise FileNotFoundError(
(
"""
---------------------------------------
Could not determine browser executable.
---------------------------------------
Browser must be installed in the default location / path!
If you are sure about the browser executable,
set it using `browser_executable_path='{}` parameter."""
).format(
"/path/to/your/browser/executable"
if is_posix
else "c:/path/to/your/browser.exe"
)
)
if getattr(self.config, "_extensions", None):
self.config.add_argument(
"--load-extension=%s"
% ",".join(str(_) for _ in self.config._extensions)
)
exe = self.config.browser_executable_path
params = self.config()
logger.debug(
"Starting\n\texecutable :%s\n\narguments:\n%s",
exe,
"\n\t".join(params),
)
if not connect_existing:
self._process: asyncio.subprocess.Process = (
await asyncio.create_subprocess_exec(
exe,
*params,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
close_fds=is_posix,
)
)
await asyncio.sleep(0.05)
self._process_pid = self._process.pid
try:
self._process_create_time = (
psutil.Process(self._process_pid).create_time()
)
except Exception:
self._process_create_time = None
await asyncio.sleep(0.05)
self._http = HTTPApi((self.config.host, self.config.port))
await asyncio.sleep(0.05)
get_registered_instances().add(self)
await asyncio.sleep(0.15)
max_attempts = 20
for attempt in range(max_attempts):
try:
self.info = ContraDict(
await self._http.get("version"), silent=True
)
except (Exception,):
if attempt == max_attempts - 1:
logger.debug("Could not start", exc_info=True)
else:
await self.sleep(0.2)
else:
break
if not self.info:
chromium = "Chromium"
if getattr(sb_config, "_cdp_browser", None):
chromium = sb_config._cdp_browser
chromium = chromium[0].upper() + chromium[1:]
message = "Failed to connect to the browser"
if not exe or not os.path.exists(exe):
message = (
"%s executable not found. Is it installed?" % chromium
)
dash_len = len(message)
dashes = "-" * dash_len
raise Exception(
"""
%s
%s
%s
""" % (dashes, message, dashes)
)
await asyncio.sleep(0.03)
self.connection = Connection(
self.info.webSocketDebuggerUrl, browser=self
)
await asyncio.sleep(0.03)
if self.config.autodiscover_targets:
logger.debug("Enabling autodiscover targets")
self.connection.handlers[cdp.target.TargetInfoChanged] = [
self._handle_target_update
]
self.connection.handlers[cdp.target.TargetCreated] = [
self._handle_target_update
]
self.connection.handlers[cdp.target.TargetDestroyed] = [
self._handle_target_update
]
self.connection.handlers[cdp.target.TargetCrashed] = [
self._handle_target_update
]
await self.connection.send(
cdp.target.set_discover_targets(discover=True)
)
await self
# self.connection.handlers[cdp.inspector.Detached] = [self.stop]
# return self
async def grant_permissions(
self,
permissions: List[str] | str,
origin: Optional[str] = None,
):
"""Grant specific permissions to the current window.
Applies to all origins if no origin is specified."""
if isinstance(permissions, str):
permissions = [permissions]
await self.connection.send(
cdp.browser.grant_permissions(permissions, origin)
)
async def grant_all_permissions(self):
"""
Grant permissions for:
audioCapture
backgroundSync
backgroundFetch
clipboardReadWrite
clipboardSanitizedWrite
displayCapture
durableStorage
geolocation
idleDetection
localFonts
midi
midiSysex
nfc
notifications
paymentHandler
periodicBackgroundSync
sensors
storageAccess
topLevelStorageAccess
videoCapture
wakeLockScreen
wakeLockSystem
windowManagement
"""
permissions = [
"audioCapture",
"backgroundSync",
"backgroundFetch",
"clipboardReadWrite",
"clipboardSanitizedWrite",
"displayCapture",
"durableStorage",
"geolocation",
"idleDetection",
"localFonts",
"midi",
"midiSysex",
"nfc",
"notifications",
"paymentHandler",
"periodicBackgroundSync",
"sensors",
"storageAccess",
"topLevelStorageAccess",
"videoCapture",
"wakeLockScreen",
"wakeLockSystem",
"windowManagement",
]
await self.connection.send(cdp.browser.grant_permissions(permissions))
async def reset_permissions(self):
"""Reset permissions for all origins on the current window."""
await self.connection.send(cdp.browser.reset_permissions())
async def tile_windows(self, windows=None, max_columns: int = 0):
import math
try:
import mss
except Exception:
pip_find_lock = fasteners.InterProcessLock(
constants.PipInstall.FINDLOCK
)
with pip_find_lock: # Prevent issues with multiple processes
shared_utils.pip_install("mss")
import mss
m = mss.mss()
screen, screen_width, screen_height = 3 * (None,)
if m.monitors and len(m.monitors) >= 1:
screen = m.monitors[0]
screen_width = screen["width"]
screen_height = screen["height"]
if not screen or not screen_width or not screen_height:
warnings.warn("No monitors detected!")
return
await self
distinct_windows = defaultdict(list)
if windows:
tabs = windows
else:
tabs = self.tabs
for _tab in tabs:
window_id, bounds = await _tab.get_window()
distinct_windows[window_id].append(_tab)
num_windows = len(distinct_windows)
req_cols = max_columns or int(num_windows * (19 / 6))
req_rows = int(num_windows / req_cols)
while req_cols * req_rows < num_windows:
req_rows += 1
box_w = math.floor((screen_width / req_cols) - 1)
box_h = math.floor(screen_height / req_rows)
distinct_windows_iter = iter(distinct_windows.values())
grid = []
for x in range(req_cols):
for y in range(req_rows):
try:
tabs = next(distinct_windows_iter)
except StopIteration:
continue
if not tabs:
continue
tab = tabs[0]
try:
pos = [x * box_w, y * box_h, box_w, box_h]
grid.append(pos)
await tab.set_window_size(*pos)
except Exception:
logger.info(
"Could not set window size. Exception => ",
exc_info=True,
)
continue
return grid
async def _get_targets(self) -> List[cdp.target.TargetInfo]:
info = await self.connection.send(
cdp.target.get_targets(), _is_update=True
)
return info
async def update_targets(self):
targets: List[cdp.target.TargetInfo]
targets = await self._get_targets()
for t in targets:
for existing_tab in self.targets:
existing_target = existing_tab.target
if existing_target.target_id == t.target_id:
existing_tab.target.__dict__.update(t.__dict__)
break
else:
self.targets.append(
Connection(
(
f"ws://{self.config.host}:{self.config.port}"
f"/devtools/page" # All types are "page"
f"/{t.target_id}"
),
target=t,
browser=self,
)
)
await asyncio.sleep(0)
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if exc_type and exc_val:
raise exc_type(exc_val)
def __iter__(self):
self._i = self.tabs.index(self.main_tab)
return self
def __reversed__(self):
return reversed(list(self.tabs))
def __next__(self):
try:
return self.tabs[self._i]
except IndexError:
del self._i
raise StopIteration
except AttributeError:
del self._i
raise StopIteration
finally:
if hasattr(self, "_i"):
if self._i != len(self.tabs):
self._i += 1
else:
del self._i
def stop(self, deconstruct=False):
if (
not hasattr(sb_config, "_closed_connection_ids")
or not isinstance(sb_config._closed_connection_ids, list)
):
sb_config._closed_connection_ids = []
connection_id = None
with suppress(Exception):
connection_id = self.connection.websocket.id.hex
close_success = False
try:
if self.connection:
asyncio.get_event_loop().create_task(self.connection.aclose())
logger.debug(
"Closed connection using get_event_loop().create_task()"
)
except RuntimeError:
if self.connection:
try:
asyncio.run(self.connection.aclose())
logger.debug("Closed the connection using asyncio.run()")
except Exception:
pass
for _ in range(3):
try:
if connection_id not in sb_config._closed_connection_ids:
self._process.terminate()
logger.debug(
"Terminated browser with pid %d successfully."
% self._process.pid
)
if connection_id:
sb_config._closed_connection_ids.append(connection_id)
close_success = True
break
except (Exception,):
try:
self._process.kill()
logger.debug(
"Killed browser with pid %d successfully."
% self._process.pid
)
break
except (Exception,):
try:
if hasattr(self, "browser_process_pid"):
os.kill(self._process_pid, 15)
logger.debug(
"Killed browser with pid %d "
"using signal 15 successfully."
% self._process.pid
)
break
except (TypeError,):
logger.info("TypeError", exc_info=True)
pass
except (PermissionError,):
logger.info(
"Browser already stopped, "
"or no permission to kill. Skip."
)
pass
except (ProcessLookupError,):
logger.info("ProcessLookupError")
pass
except (Exception,):
raise
self._process = None
self._process_pid = None
if (
hasattr(sb_config, "_xvfb_users")
and isinstance(sb_config._xvfb_users, int)
and close_success
and hasattr(sb_config, "_virtual_display")
and sb_config._virtual_display
):
sb_config._xvfb_users -= 1
if sb_config._xvfb_users < 0:
sb_config._xvfb_users = 0
if (
shared_utils.is_linux()
and (
hasattr(sb_config, "_virtual_display")
and sb_config._virtual_display
and hasattr(sb_config._virtual_display, "stop")
)
and sb_config._xvfb_users == 0
):
try:
sb_config._virtual_display.stop()
sb_config._virtual_display = None
sb_config.headless_active = False
except AttributeError:
pass