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
U
[±dfEã@s²UddlZddlZddlZddlZddlZddlZddlZddlmZddl    m
Z
ddl    m Z ddl m Z ddlZddlmZddlmZe d¡Ze d    ¡Zd
Zdadad d „Ze d ¡aejed<e d¡aejed<dd„Zdd„Z e dd „ƒZ!e dd„ƒZ"dd„Z#dd„Z$Gdd„de%ƒZ&Gdd„deƒZ'Gdd „d e%ƒZ(e Gd!d"„d"ƒƒZ)Gd#d$„d$ƒZ*Gd%d&„d&e*ƒZ+Gd'd(„d(e*ej,j-ƒZ.e
ej/e.d)œd*d+„ƒZ0e.d)œd,d-„Z1dS).éN)Ú defaultdict)Úasynccontextmanager)Úcontextmanager)Ú    dataclass)ÚConnectionClosed)Úconnect_websocket_urlZtrio_cdpÚTicCs¨|ad}zt |›|›¡atWStk
r¢t t¡jd     d¡}t
dd„|  ¡Dƒƒ}t dd„|Dƒƒ}t  t¡}| d|›¡t |›|›¡atYSXdS)    z^Attempt to load the current latest available devtools into the module
    cache for use later.z$selenium.webdriver.common.devtools.véÚdevtoolscss|]}| ¡r|jVqdS©N)Úis_dirÚname)Ú.0Úf©rúYd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\selenium/webdriver/common/bidi/cdp.pyÚ    <genexpr>Asz"import_devtools.<locals>.<genexpr>css|]}t|dd…ƒVqdS)r    N)Úint)rÚxrrrrBsz%Falling back to loading `devtools`: vN)ÚversionÚ    importlibÚ import_moduler
ÚModuleNotFoundErrorÚpathlibÚPathÚ__file__ÚparentsÚjoinpathÚtupleÚiterdirÚmaxÚloggingÚ    getLoggerÚ__name__Údebug)ÚverÚbaseZ devtools_pathÚversionsZlatestZselenium_loggerrrrÚimport_devtools3s
r(Úconnection_contextÚ_connection_contextÚsession_contextÚ_session_contextcCs2z
t ¡WStk
r,t|›dƒ‚YnXdS)zLook up the current connection.
 
    If there is no current connection, raise a ``RuntimeError`` with a
    helpful message.
    z*() must be called in a connection context.N)r*ÚgetÚ LookupErrorÚ RuntimeError©Úfn_namerrrÚget_connection_contextMs
r2cCs2z
t ¡WStk
r,t|›dƒ‚YnXdS)z{Look up the current session.
 
    If there is no current session, raise a ``RuntimeError`` with a
    helpful message.
    z'() must be called in a session context.N)r,r-r.r/r0rrrÚget_session_contextYs
r3c    cs&t |¡}z
dVW5t |¡XdS)zbThis context manager installs ``connection`` as the session context for
    the current Trio task.N)r*ÚsetÚreset)Ú
connectionÚtokenrrrr)es
 
c    cs&t |¡}z
dVW5t |¡XdS)z_This context manager installs ``session`` as the session context for the
    current Trio task.N)r,r4r5)Úsessionr7rrrr+ps
 
cCstjd|dadS)zõInstall ``connection`` in the root context so that it will become the
    default connection for all tasks.
 
    This is generally not recommended, except it may be necessary in
    certain use cases such as running inside Jupyter notebook.
    r*©ÚdefaultN)Ú contextvarsÚ
ContextVarr*)r6rrrÚset_global_connection{sr=cCstjd|dadS)zïInstall ``session`` in the root context so that it will become the
    default session for all tasks.
 
    This is generally not recommended, except it may be necessary in
    certain use cases such as running inside Jupyter notebook.
    r,r9N)r;r<r,)r8rrrÚset_global_session†sr>c@s eZdZdZdd„Zdd„ZdS)Ú BrowserErrorzgThis exception is raised when the browser's response to a command
    indicates that an error occurred.cCs$|d|_|d|_| d¡|_dS)NÚcodeÚmessageÚdata)r@rAr-Údetail)ÚselfÚobjrrrÚ__init__•s
 
zBrowserError.__init__cCsd|j›d|j›d|j›S)NzBrowserError<code=z     message=z> )r@rArC©rDrrrÚ__str__šszBrowserError.__str__N)r#Ú
__module__Ú __qualname__Ú__doc__rFrHrrrrr?‘sr?c@s eZdZdZdd„Zdd„ZdS)ÚCdpConnectionClosedzARaised when a public method is called on a closed CDP connection.cCs
||_dS)zfConstructor.
 
        :param reason:
        :type reason: wsproto.frame_protocol.CloseReason
        N)Úreason)rDrMrrrrF¡szCdpConnectionClosed.__init__cCs|jj›d|j›dS)zReturn representation.ú<ú>)Ú    __class__r#rMrGrrrÚ__repr__©szCdpConnectionClosed.__repr__N)r#rIrJrKrFrQrrrrrLžsrLc@seZdZdZdS)Ú InternalErrorzfThis exception is only raised when there is faulty logic in TrioCDP or
    the integration with PyCDP.N)r#rIrJrKrrrrrR®srRc@s eZdZUdZdZejed<dS)Ú CmEventProxyz³A proxy object returned by :meth:`CdpBase.wait_for()``.
 
    After the context manager executes, this proxy object will have a
    value set that contains the returned event.
    NÚvalue)r#rIrJrKrTÚtypingÚAnyÚ__annotations__rrrrrS³s
rSc@s~eZdZdd„Zejeeejfedœdd„Z    ddœdd    „Z
e dej eej ed
fd œd d „ƒZdd„Zdd„Zdd„Zd
S)ÚCdpBasecCs6||_||_||_ttƒ|_t ¡|_i|_    i|_
dSr ) ÚwsÚ
session_idÚ    target_idrr4ÚchannelsÚ    itertoolsÚcountÚid_iterÚ inflight_cmdÚinflight_result©rDrYrZr[rrrrF¿s
 
zCdpBase.__init__)ÚcmdÚreturnc
Ãs¼t|jƒ}t ¡}||f|j|<t|ƒ}||d<|jr@|j|d<t |¡}z|j     |¡IdHWn.t
k
rŽ}zt |j ƒd‚W5d}~XYnX|  ¡IdH|j |¡}t|tƒr¸|‚|S)z…Execute a command on the server and wait for the result.
 
        :param cmd: any CDP command
        :returns: a CDP result
        ÚidÚ    sessionIdN)Únextr_ÚtrioÚEventr`rZÚjsonÚdumpsrYÚ send_messageÚWsConnectionClosedrLrMÚwaitraÚpopÚ
isinstanceÚ    Exception)rDrcÚcmd_idZ    cmd_eventÚrequestZ request_strZwccÚresponserrrÚexecuteÈs"
 
 
 
zCdpBase.executeé
)Ú buffer_sizecGs,t |¡\}}|D]}|j| |¡q|S)zXReturn an async iterator that iterates over events matching the
        indicated types.)rhÚopen_memory_channelr\Úadd)rDrwZ event_typesÚsenderÚreceiverÚ
event_typerrrÚlistenàszCdpBase.listenN)r|rdc
Cs`t |¡\}}|j| |¡tƒ}|V|4IdHš| ¡IdH}W5QIdHRX||_dS)zçWait for an event of the given type and return it.
 
        This is an async context manager, so you should open it inside
        an async with block. The block will not exit until the indicated
        event is received.
        N)rhrxr\ryrSZreceiverT)rDr|rwrzr{ÚproxyÚeventrrrÚwait_forèszCdpBase.wait_forcCs"d|kr| |¡n
| |¡dS)zUHandle incoming WebSocket data.
 
        :param dict data: a JSON dictionary
        reN)Ú_handle_cmd_responseÚ _handle_event)rDrBrrrÚ _handle_dataøs zCdpBase._handle_datac
Csº|d}z|j |¡\}}Wn&tk
rBt d|›¡YdSXd|kr`t|dƒ|j|<nNz| |d¡}tdƒ‚Wn(t    k
r¢}z
|j
}W5d}~XYnX||j|<|  ¡dS)zÃHandle a response to a command. This will set an event flag that
        will return control to the task that called the command.
 
        :param dict data: response as a JSON dictionary
        rez5Got a message with a command ID that does not exist: NÚerrorÚresultz<The command's generator function did not exit when expected!) r`roÚKeyErrorÚloggerÚwarningr?raÚsendrRÚ StopIterationrTr4)rDrBrrrcrrtÚexitZreturn_rrrrs 
zCdpBase._handle_cmd_responsec    Cs¢tj |¡}t d|¡tƒ}|jt|ƒD]V}z| |¡Wq,t    j
k
rbt  d||¡Yq,t    j k
r€|  |¡Yq,Xq,|rž|jt|ƒ|8<dS)zOHandle an event.
 
        :param dict data: event as a JSON dictionary
        zReceived event: %sz0Unable to send event "%r" due to full channel %sN)r
ÚutilZparse_json_eventr‡r$r4r\ÚtypeZ send_nowaitrhZ
WouldBlockr„ZBrokenResourceErrorry)rDrBrZ    to_removerzrrrr‚s  zCdpBase._handle_event)rv)r#rIrJrFrUÚ    GeneratorÚdictrrVrur}rÚTypeÚAsyncGeneratorrSr€rƒrr‚rrrrrX¾s    $
rXcs8eZdZdZ‡fdd„Zedd„ƒZedd„ƒZ‡ZS)Ú
CdpSessionz¢Contains the state for a CDP session.
 
    Generally you should not instantiate this object yourself; you should call
    :meth:`CdpConnection.open_session`.
    cs4tƒ |||¡d|_t ¡|_d|_t ¡|_dS)z´Constructor.
 
        :param trio_websocket.WebSocketConnection ws:
        :param devtools.target.SessionID session_id:
        :param devtools.target.TargetID target_id:
        rN)ÚsuperrFÚ_dom_enable_countrhÚLockÚ_dom_enable_lockÚ_page_enable_countÚ_page_enable_lockrb©rPrrrF8s
 
zCdpSession.__init__c
Cs¦|j4IdHš4|jd7_|jdkr>| tj ¡¡IdHW5QIdHRXdV|j4IdHš4|jd8_|jdkr’| tj ¡¡IdHW5QIdHRXdS)zåA context manager that executes ``dom.enable()`` when it enters and
        then calls ``dom.disable()``.
 
        This keeps track of concurrent callers and only disables DOM
        events when all callers have exited.
        Nr    r)r–r”rur
ÚdomÚenableÚdisablerGrrrÚ
dom_enableFs    
&
zCdpSession.dom_enablec
Cs¦|j4IdHš4|jd7_|jdkr>| tj ¡¡IdHW5QIdHRXdV|j4IdHš4|jd8_|jdkr’| tj ¡¡IdHW5QIdHRXdS)zöA context manager that executes ``page.enable()`` when it enters and
        then calls ``page.disable()`` when it exits.
 
        This keeps track of concurrent callers and only disables page
        events when all callers have exited.
        Nr    r)r˜r—rur
Úpager›rœrGrrrÚ page_enable[s    
&
zCdpSession.page_enable)    r#rIrJrKrFrrrŸÚ __classcell__rrr™rr’1s  
r’csVeZdZdZ‡fdd„Zdd„Zeeje    dœdd„ƒZ
d    dœd
d „Z d d „Z ‡Z S)Ú CdpConnectiona%Contains the connection state for a Chrome DevTools Protocol server.
 
    CDP can multiplex multiple "sessions" over a single connection. This
    class corresponds to the "root" session, i.e. the implicitly created
    session that has no session ID. This class is responsible for
    reading incoming WebSocket messages and forwarding them to the
    corresponding session, as well as handling messages targeted at the
    root session itself. You should generally call the
    :func:`open_cdp()` instead of instantiating this class directly.
    cstƒj|dddi|_dS)zLConstructor.
 
        :param trio_websocket.WebSocketConnection ws:
        N)rZr[)r“rFÚsessions)rDrYr™rrrF}szCdpConnection.__init__cÃs|j ¡IdHdS)atClose the underlying WebSocket connection.
 
        This will cause the reader task to gracefully exit when it tries
        to read the next message from the WebSocket. All of the public
        APIs (``execute()``, ``listen()``, etc.) will raise
        ``CdpConnectionClosed`` after the CDP connection is closed. It
        is safe to call this multiple times.
        N)rYÚacloserGrrrr£…s    zCdpConnection.aclose©rdc    Cs.| |¡IdH}t|ƒ |VW5QRXdS)a This context manager opens a session and enables the "simple" style
        of calling CDP APIs.
 
        For example, inside a session context, you can call ``await
        dom.get_document()`` and it will execute on the current session
        automatically.
        N)Úconnect_sessionr+)rDr[r8rrrÚ open_sessions    
zCdpConnection.open_sessionr’cÃs6| tj |d¡¡IdH}t|j||ƒ}||j|<|S)zLReturns a new :class:`CdpSession` connected to the specified
        target.TN)rur
ÚtargetZattach_to_targetr’rYr¢)rDr[rZr8rrrr¥s
zCdpConnection.connect_sessioncÃsÖz|j ¡IdH}Wntk
r,YqÒYnXzt |¡}Wn&tjk
rbtdd|dœƒ‚YnXt d|¡d|krÆt    j
  |d¡}z|j |}Wn"t k
r¸td|›ƒ‚YnX| |¡q| |¡qdS)zxRuns in the background and handles incoming messages: dispatching
        responses to commands and events to listeners.NiD€ÿÿzClient received invalid JSON)r@rArBzReceived message %rrfz/Browser sent a message for an invalid session: )rYZ get_messagermrjÚloadsÚJSONDecodeErrorr?r‡r$r
r§Z    SessionIDr¢r†rƒ)rDrArBrZr8rrrÚ _reader_task¦s"
  zCdpConnection._reader_task)r#rIrJrKrFr£rrUÚ AsyncIteratorr’r¦r¥rªr rrr™rr¡qs       r¡r¤c
Csft ¡4IdHšF}t||ƒIdH}zt|ƒ |VW5QRXW5| ¡IdHXW5QIdHRXdS)aèThis async context manager opens a connection to the browser specified
    by ``url`` before entering the block, then closes the connection when the
    block exits.
 
    The context manager also sets the connection as the default
    connection for the current task, so that commands like ``await
    target.get_targets()`` will run on this connection automatically. If
    you want to use multiple connections concurrently, it is recommended
    to open each on in a separate task.
    N)rhZ open_nurseryÚ connect_cdpr£r))ÚurlÚnurseryÚconnrrrÚopen_cdpÃs 
r°cÃs,t||tdIdH}t|ƒ}| |j¡|S)a¯Connect to the browser specified by ``url`` and spawn a background task
    in the specified nursery.
 
    The ``open_cdp()`` context manager is preferred in most situations.
    You should only use this function if you need to specify a custom
    nursery. This connection is not automatically closed! You can either
    use the connection object as a context manager (``async with
    conn:``) or else call ``await conn.aclose()`` on it when you are
    done with it. If ``set_context`` is True, then the returned
    connection will be installed as the default connection for the
    current task. This argument is for unusual use cases, such as
    running inside of a notebook.
    )Zmax_message_sizeN)rÚMAX_WS_MESSAGE_SIZEr¡Z
start_soonrª)r®r­rYZcdp_connrrrr¬Ùs r¬)2r;rr]rjr!rrUÚ collectionsrÚ
contextlibrrZ dataclassesrrhZtrio_websocketrrmrr"r‡ÚTypeVarrr±r
rr(r<r*rWr,r2r3r)r+r=r>rqr?rLrRrSrXr’ÚabcZ AsyncResourcer¡r«r°r¬rrrrÚ<module>sR      
 
 
 
 
 
 
s@R