
    $)f                     D   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl$m&Z& ddl$m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl0m2Z2 dd l3m4Z4 dd!l5m6Z6 dd"l7m8Z8 dd#l9m:Z: dd$l;m<Z< dd%l=m>Z> da?d& Z@d' ZAd1d(ZBd)ee   d*efd+ZC G d, d-e.      ZD G d/ d0eD      ZEy)2zThe WebDriver implementation.    N)ABCMeta)	b64decode)urlsafe_b64encode)asynccontextmanager)contextmanager)import_module)Dict)List)Optional)Union)InvalidArgumentException)JavascriptException)NoSuchCookieException)NoSuchElementException)WebDriverException)By)BaseOptions)PrintOptions)Timeouts)
Credential)VirtualAuthenticatorOptions)required_virtual_authenticator)
RelativeBy   )BidiConnection)Command)ErrorHandler)FileDetector)LocalFileDetector)Mobile)RemoteConnection)	ScriptKey)
ShadowRoot)SwitchTo)
WebElementc                  (    t         st        d      a y y )Nz"selenium.webdriver.common.bidi.cdp)cdpr        `/var/www/html/flask-app/venv/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py
import_cdpr+   E   s    @A r)   c                 z    t        j                  |       } i }| j                         D ]
  \  }}|||<    di g|diS )a  Makes a W3C alwaysMatch capabilities object.

    Filters out capability names that are not in the W3C spec. Spec-compliant
    drivers will reject requests containing unknown capability names.

    Moves the Firefox profile, if present, from the old location to the new Firefox
    options object.

    :Args:
     - caps - A dictionary of capabilities requested by the caller.
    capabilities)
firstMatchalwaysMatch)copydeepcopyitems)capsalways_matchkvs       r*   _create_capsr7   K   sK     ==DL

 1QB4MNNr)   c                      ddl m} ddlm} ddlm} ddlm} ||||g}t         fd|D        t              }	 |	|||      S )Nr   )ChromeRemoteConnection)EdgeRemoteConnection)FirefoxRemoteConnection)SafariRemoteConnectionc              3   `   K   | ]%  }|j                   j                  d       k(  s"| ' yw)browserNameN)browser_nameget).0cr-   s     r*   	<genexpr>z(get_remote_connection.<locals>.<genexpr>e   s(     _!Q^^|?O?OP]?^-^A_s   #..)
keep_aliveignore_proxy)
+selenium.webdriver.chrome.remote_connectionr9   )selenium.webdriver.edge.remote_connectionr:   ,selenium.webdriver.firefox.remote_connectionr;   +selenium.webdriver.safari.remote_connectionr<   nextr!   )
r-   command_executorrD   ignore_local_proxyr9   r:   r;   r<   
candidateshandlers
   `         r*   get_remote_connectionrO   ^   sD    RNTR(*>@VXopJ_z_aqrG#
I[\\r)   optionsreturnc                    di i}g }| D ]!  }|j                  |j                                # t        |      }i }t        |      D ]n  }|}|dz   |k  s||   j	                         }|D ]I  }	|	||dz      j	                         v s||   |	   ||dz      |	   k(  s1|j                  |	||   |	   i       K p i }
|j                         D ]
  \  }}||
|<    |D ]  }|
D ]  }||=   |
|d   d<   ||d   d<   |S )Nr-   r   r/   r.   )appendto_capabilitieslenrangekeysupdater2   )rP   r-   optsopt	opts_sizesamesiesi	min_index
first_keyskysalwaysr5   r6   s                r*   create_matchesrb   j   sW   "B'LD +C'')*+D	IH 9 E	q59i--/J! E$q1u+**,,Is+tAE{3/?? d9oc.B(CDEE F  1q	   	A!	 39L /15L .r)   c                       e Zd ZdZy)BaseWebDriverzAbstract Base Class for all Webdriver subtypes.

    ABC's allow custom implementations of Webdriver to be registered so
    that isinstance type checks will succeed.
    N)__name__
__module____qualname____doc__r(   r)   r*   rd   rd      s    r)   rd   )	metaclassc                      e Zd ZdZeZeZ	 	 	 	 dfdee	e
e	   f   ddfdZd Zd Zdej                  ej                   e      d	ej                  e   d
ej                  ej&                     fdZed        Zedefd       Zedefd       Zd Zd ZdeddfdZd Z dedefdZ!d Z"dgdededefdZ#deddfdZ$edefd       Z%dgdede&fdZ'de&ddfd Z(de
e   fd!Z)d" Z*defd#Z+edefd$       Z,edefd%       Z-dhd&Z.dhd'Z/edefd(       Z0ede
e   fd)       Z1dhd*Z2dhd+Z3dhd,Z4dgd-ee5   defd.Z6ede7fd/       Z8dhd0Z9dhd1Z:dhd2Z;de
e   fd3Z<dej                  ejz                     fd4Z>dhd5Z?dhd6Z@dhd7ZAd8eBddfd9ZCd8eBddfd:ZDd8eBddfd;ZEedeFfd<       ZGeGj                  dhd=       ZGeIj                  dfd>ee   defd?ZKeIj                  dfd>ee   de
e   fd@ZLedefdA       ZMdeNfdBZOdeNfdCZPdeQfdDZRdefdEZSdidFeddfdGZTdidFedefdHZUdidFedefdIZVdidefdJZWdFeddfdKZXdefdLZYdjdefdMZZede[fdN       Z\e\j                  dhdO       Z\edP        Z]e]j                  dhdQ       Z]edR        Z^dS Z_e`dT        ZadU ZbdecddfdVZdedefdW       ZeefdhdX       ZgefdYehddfdZ       Ziefde
eh   fd[       Zjefd\eeekf   ddfd]       Zlefdhd^       Zmefd_eNddfd`       ZndefdaZodbedceddfddZpdhdeZqy)k	WebDrivera  Controls a browser by sending commands to a remote server. This server
    is expected to be running the WebDriver wire protocol as defined at
    https://www.selenium.dev/documentation/legacy/json_wire_protocol/.

    :Attributes:
     - session_id - String ID of the browser session started and controlled by this WebDriver.
     - capabilities - Dictionary of effective capabilities of this browser session as returned
         by the remote server. See https://www.selenium.dev/documentation/legacy/desired_capabilities/
     - command_executor - remote_connection.RemoteConnection object used to execute commands.
     - error_handler - errorhandler.ErrorHandler object used to handle errors.
    NrP   rQ   c                    t        |t              rt        |      }d}n|j                         }|j                  }|| _        t        | j
                  t        t        f      rt        ||||      | _        d| _	        d| _
        i | _        i | _        t               | _        t        |       | _        t#        |       | _        |xs
 t'               | _        d| _        | j-                          | j/                  |       y)a  Create a new driver that will issue commands using the wire
        protocol.

        :Args:
         - command_executor - Either a string representing URL of the remote server or a custom
             remote_connection.RemoteConnection object. Defaults to 'http://127.0.0.1:4444/wd/hub'.
         - keep_alive - Whether to configure remote_connection.RemoteConnection to use
             HTTP keep-alive. Defaults to True.
         - file_detector - Pass custom file detector object during instantiation. If None,
             then default LocalFileDetector() will be used.
         - options - instance of a driver options.Options class
        F)rK   rD   rL   TN)
isinstancelistrb   rT   _ignore_local_proxyrK   strbytesrO   
_is_remote
session_idr3   pinned_scriptsr   error_handlerr$   
_switch_tor    _mobiler   file_detector_authenticator_idstart_clientstart_session)selfrK   rD   rx   rP   r-   ro   s          r*   __init__zWebDriver.__init__   s    ( gt$)'2L"'"224L")"="= 0d++c5\:$9!1%#6	%D! 	 )^"4.d|*A.?.A!%<(r)   c                 z    dt        |       j                   dt        |       j                   d| j                   dS )N<.z (session="z")>)typerf   re   rs   r|   s    r*   __repr__zWebDriver.__repr__   s9    4:(()4:+>+>*?{4??J[[^__r)   c                     | S Nr(   r   s    r*   	__enter__zWebDriver.__enter__   s    r)   exc_typeexc	tracebackc                 $    | j                          y r   )quit)r|   r   r   r   s       r*   __exit__zWebDriver.__exit__   s     			r)   c              /      K   d}t        | j                  |      s| j                  } ||i || _        	 d |r|| _        yy# |r|| _        w w xY ww)a  Overrides the current file detector (if necessary) in limited
        context. Ensures the original file detector is set afterwards.

        Example::

            with webdriver.file_detector_context(UselessFileDetector):
                someinput.send_keys('/etc/hosts')

        :Args:
         - file_detector_class - Class of the desired file detector. If the class is different
             from the current file_detector, then the class is instantiated with args and kwargs
             and used as a file detector during the duration of the context manager.
         - args - Optional arguments that get passed to the file detector class during
             instantiation.
         - kwargs - Keyword arguments, passed the same way as args.
        N)rm   rx   )r|   file_detector_classargskwargslast_detectors        r*   file_detector_contextzWebDriver.file_detector_context   sc     $ $,,.AB ..M!4d!Ef!ED	3%2" }%2" s   2AA AAAc                     | j                   S r   )rw   r   s    r*   mobilezWebDriver.mobile   s    ||r)   c                 R    d| j                   v r| j                   d   S t        d      )zReturns the name of the underlying browser for this instance.

        :Usage:
            ::

                name = driver.name
        r>   z1browserName not specified in session capabilities)r3   KeyErrorr   s    r*   namezWebDriver.name   s+     DII%99]++JKKr)   c                      y)zxCalled before starting a new session.

        This method may be overridden to define custom startup behavior.
        Nr(   r   s    r*   rz   zWebDriver.start_client  s    
 	r)   c                      y)zCalled after executing a quit command.

        This method may be overridden to define custom shutdown
        behavior.
        Nr(   r   s    r*   stop_clientzWebDriver.stop_client  s     	r)   r-   c                     t        |      }| j                  t        j                  |      d   }|j	                  d      | _        |j	                  d      | _        y)zCreates a new session with the desired capabilities.

        :Args:
         - capabilities - a capabilities dict to start the session with.
        value	sessionIdr-   N)r7   executer   NEW_SESSIONr@   rs   r3   )r|   r-   r3   responses       r*   r{   zWebDriver.start_session  sI     L)<< 3 3T:7C",,{3LL0	r)   c                 `    t        |t              r0i }|j                         D ]  \  }} j                  |      ||<    |S t        | j                        rd|j
                  iS t        | j                        rd|j
                  iS t        |t              rt         fd|D              S |S )N#element-6066-11e4-a52e-4f735466cecf"shadow-6066-11e4-a52e-4f735466cecfc              3   @   K   | ]  }j                  |        y wr   )_wrap_valuerA   itemr|   s     r*   rC   z(WebDriver._wrap_value.<locals>.<genexpr>3  s     A4((.A   )rm   dictr2   r   _web_element_clsid_shadowroot_clsrn   )r|   r   	convertedkeyvals   `    r*   r   zWebDriver._wrap_value(  s    eT"I!KKM 7S!%!1!1#!6	#7eT2239588DDeT1128%((CCeT"A5AAAr)   
element_idc                 &    | j                  | |      S )z6Creates a web element with the specified `element_id`.)r   )r|   r   s     r*   create_web_elementzWebDriver.create_web_element6  s    $$T:66r)   c                 .    t        |t              r_d|v r j                  |d         S d|v r j                   |d         S |j	                         D ]  \  }} j                  |      ||<    |S t        |t              rt         fd|D              S |S )Nr   r   c              3   @   K   | ]  }j                  |        y wr   )_unwrap_valuer   s     r*   rC   z*WebDriver._unwrap_value.<locals>.<genexpr>D  s     CT**40Cr   )rm   r   r   r   r2   r   rn   )r|   r   r   r   s   `   r*   r   zWebDriver._unwrap_value:  s    eT"4=..u5Z/[\\3u<++D%8\2]^^!KKM 5S!//4c
5LeT"CUCCCr)   driver_commandparamsc                 b   | j                  |      }| j                  r$|sd| j                  i}nd|vr| j                  |d<   | j                  j                  ||      }|rA| j                  j                  |       | j                  |j                  dd            |d<   |S dd| j                  dS )aH  Sends a command to be executed by a command.CommandExecutor.

        :Args:
         - driver_command: The name of the command to execute as a string.
         - params: A dictionary of named parameters to send with the command.

        :Returns:
          The command's JSON response loaded into a dictionary object.
        r   r   Nr   )successr   r   )r   rs   rK   r   ru   check_responser   r@   )r|   r   r   r   s       r*   r   zWebDriver.executeG  s     !!&)??%t7F*&*oo{#((00H--h7 $ 2 28<<3N OHWO t$//JJr)   urlc                 H    | j                  t        j                  d|i       y)z0Loads a web page in the current browser session.r   N)r   r   GET)r|   r   s     r*   r@   zWebDriver.getb  s    W[[5#,/r)   c                 `    | j                  t        j                        j                  dd      S )zuReturns the title of the current page.

        :Usage:
            ::

                title = driver.title
        r    )r   r   	GET_TITLEr@   r   s    r*   titlezWebDriver.titlef  s&     ||G--.227B??r)   scriptc                 N    t        |      }|| j                  |j                  <   |S )zUStore common javascript scripts to be executed later by a unique
        hashable ID.)r"   rt   r   )r|   r   
script_keyscript_key_instances       r*   
pin_scriptzWebDriver.pin_scriptq  s,     (
36</223""r)   r   c                     	 | j                   j                  |j                         y# t        $ r t        d| d| j                          dw xY w)z$Remove a pinned script from storage.zNo script with key: z existed in N)rt   popr   r   )r|   r   s     r*   unpinzWebDriver.unpinx  sT    	k##JMM2 	k1*\$J]J]I^_`fjj	ks	   %( &Ac                 ,    t        | j                        S r   )rn   rt   r   s    r*   get_pinned_scriptszWebDriver.get_pinned_scripts  s    D''((r)   c                     t        |t              r	 | j                  |j                     }t        |      }t        j                  }| j                  |||d      d   S # t        $ r t        d      w xY w)a'  Synchronously Executes JavaScript in the current window/frame.

        :Args:
         - script: The JavaScript to execute.
         - \*args: Any applicable arguments for your JavaScript.

        :Usage:
            ::

                driver.execute_script('return document.title;')
        z Pinned script could not be foundr   r   r   )
rm   r"   rt   r   r   r   rn   r   W3C_EXECUTE_SCRIPTr   r|   r   r   converted_argscommands        r*   execute_scriptzWebDriver.execute_script  sy     fi(N,,VYY7 d,,||G%OPQXYY  N)*LMMNs   A A3c                 h    t        |      }t        j                  }| j                  |||d      d   S )a  Asynchronously Executes JavaScript in the current window/frame.

        :Args:
         - script: The JavaScript to execute.
         - \*args: Any applicable arguments for your JavaScript.

        :Usage:
            ::

                script = "var callback = arguments[arguments.length - 1]; " \
                         "window.setTimeout(function(){ callback('timeout') }, 3000);"
                driver.execute_async_script(script)
        r   r   )rn   r   W3C_EXECUTE_SCRIPT_ASYNCr   r   s        r*   execute_async_scriptzWebDriver.execute_async_script  s4     d22||G%OPQXYYr)   c                 F    | j                  t        j                        d   S )znGets the URL of the current page.

        :Usage:
            ::

                driver.current_url
        r   )r   r   GET_CURRENT_URLr   s    r*   current_urlzWebDriver.current_url       ||G334W==r)   c                 F    | j                  t        j                        d   S )zqGets the source of the current page.

        :Usage:
            ::

                driver.page_source
        r   )r   r   GET_PAGE_SOURCEr   s    r*   page_sourcezWebDriver.page_source  r   r)   c                 B    | j                  t        j                         y)zcCloses the current window.

        :Usage:
            ::

                driver.close()
        N)r   r   CLOSEr   s    r*   closezWebDriver.close  s     	W]]#r)   c                     	 | j                  t        j                         | j                          | j                  j                          y# | j                          | j                  j                          w xY w)z|Quits the driver and closes every associated window.

        :Usage:
            ::

                driver.quit()
        N)r   r   QUITr   rK   r   r   s    r*   r   zWebDriver.quit  sW    	*LL&!!'') !!'')s   A ,A8c                 F    | j                  t        j                        d   S )zReturns the handle of the current window.

        :Usage:
            ::

                driver.current_window_handle
        r   )r   r   W3C_GET_CURRENT_WINDOW_HANDLEr   s    r*   current_window_handlezWebDriver.current_window_handle  s     ||GAAB7KKr)   c                 F    | j                  t        j                        d   S )zReturns the handles of all windows within the current session.

        :Usage:
            ::

                driver.window_handles
        r   )r   r   W3C_GET_WINDOW_HANDLESr   s    r*   window_handleszWebDriver.window_handles       ||G::;GDDr)   c                 H    t         j                  }| j                  |d       y)z5Maximizes the current window that webdriver is using.N)r   W3C_MAXIMIZE_WINDOWr   )r|   r   s     r*   maximize_windowzWebDriver.maximize_window  s    --Wd#r)   c                 B    | j                  t        j                         y)z<Invokes the window manager-specific 'full screen' operation.N)r   r   FULLSCREEN_WINDOWr   s    r*   fullscreen_windowzWebDriver.fullscreen_window  s    W../r)   c                 B    | j                  t        j                         y)z9Invokes the window manager-specific 'minimize' operation.N)r   r   MINIMIZE_WINDOWr   s    r*   minimize_windowzWebDriver.minimize_window  s    W,,-r)   print_optionsc                 p    i }|r|j                         }| j                  t        j                  |      d   S )zTakes PDF of the current page.

        The driver makes a best effort to return a PDF based on the
        provided parameters.
        r   )to_dictr   r   
PRINT_PAGE)r|   r   rP   s      r*   
print_pagezWebDriver.print_page  s6     #++-G||G..8AAr)   c                     | j                   S )a<  
        :Returns:
            - SwitchTo: an object containing all options to switch focus into

        :Usage:
            ::

                element = driver.switch_to.active_element
                alert = driver.switch_to.alert
                driver.switch_to.default_content()
                driver.switch_to.frame('frame_name')
                driver.switch_to.frame(1)
                driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0])
                driver.switch_to.parent_frame()
                driver.switch_to.window('main')
        )rv   r   s    r*   	switch_tozWebDriver.switch_to	  s    $ r)   c                 B    | j                  t        j                         y)zvGoes one step backward in the browser history.

        :Usage:
            ::

                driver.back()
        N)r   r   GO_BACKr   s    r*   backzWebDriver.back       	W__%r)   c                 B    | j                  t        j                         y)zxGoes one step forward in the browser history.

        :Usage:
            ::

                driver.forward()
        N)r   r   
GO_FORWARDr   s    r*   forwardzWebDriver.forward(  s     	W''(r)   c                 B    | j                  t        j                         y)zfRefreshes the current page.

        :Usage:
            ::

                driver.refresh()
        N)r   r   REFRESHr   s    r*   refreshzWebDriver.refresh2  r   r)   c                 F    | j                  t        j                        d   S )zReturns a set of dictionaries, corresponding to cookies visible in
        the current session.

        :Usage:
            ::

                driver.get_cookies()
        r   )r   r   GET_ALL_COOKIESr   s    r*   get_cookieszWebDriver.get_cookies=  r   r)   c                     t        j                  t              5  | j                  t        j
                  d|i      d   cddd       S # 1 sw Y   yxY w)zGet a single cookie by name. Returns the cookie if found, None if
        not.

        :Usage:
            ::

                driver.get_cookie('my_cookie')
        r   r   N)
contextlibsuppressr   r   r   
GET_COOKIEr|   r   s     r*   
get_cookiezWebDriver.get_cookieH  sL       !67 	M<< 2 2VTNCGL	M 	M 	Ms   %A		Ac                 H    | j                  t        j                  d|i       y)zDeletes a single cookie with the given name.

        :Usage:
            ::

                driver.delete_cookie('my_cookie')
        r   N)r   r   DELETE_COOKIEr
  s     r*   delete_cookiezWebDriver.delete_cookieU  s     	W**VTN;r)   c                 B    | j                  t        j                         y)zDelete all cookies in the scope of the session.

        :Usage:
            ::

                driver.delete_all_cookies()
        N)r   r   DELETE_ALL_COOKIESr   s    r*   delete_all_cookieszWebDriver.delete_all_cookies_  s     	W//0r)   c                     d|v r,|d   dv sJ | j                  t        j                  d|i       y| j                  t        j                  d|i       y)ag  Adds a cookie to your current session.

        :Args:
         - cookie_dict: A dictionary object, with required keys - "name" and "value";
            optional keys - "path", "domain", "secure", "httpOnly", "expiry", "sameSite"

        :Usage:
            ::

                driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
                driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'})
                driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure' : True})
                driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'sameSite' : 'Strict'})
        sameSite)StrictLaxNonecookieN)r   r   
ADD_COOKIE)r|   cookie_dicts     r*   
add_cookiezWebDriver.add_cookiei  sO     $z*.GGGLL++h-DELL++h-DEr)   time_to_waitc           	      r    | j                  t        j                  dt        t	        |      dz        i       y)a  Sets a sticky timeout to implicitly wait for an element to be found,
        or a command to complete. This method only needs to be called one time
        per session. To set the timeout for calls to execute_async_script, see
        set_script_timeout.

        :Args:
         - time_to_wait: Amount of time to wait (in seconds)

        :Usage:
            ::

                driver.implicitly_wait(30)
        implicit  Nr   r   SET_TIMEOUTSintfloatr|   r  s     r*   implicitly_waitzWebDriver.implicitly_wait  s-     	W))JE,<ORV<V8W+XYr)   c           	      r    | j                  t        j                  dt        t	        |      dz        i       y)a!  Set the amount of time that the script should wait during an
        execute_async_script call before throwing an error.

        :Args:
         - time_to_wait: The amount of time to wait (in seconds)

        :Usage:
            ::

                driver.set_script_timeout(30)
        r   r  Nr  r#  s     r*   set_script_timeoutzWebDriver.set_script_timeout  s-     	W))Hc%:MPT:T6U+VWr)   c           	          	 | j                  t        j                  dt        t	        |      dz        i       y# t
        $ r2 | j                  t        j                  t	        |      dz  dd       Y yw xY w)zSet the amount of time to wait for a page load to complete before
        throwing an error.

        :Args:
         - time_to_wait: The amount of time to wait

        :Usage:
            ::

                driver.set_page_load_timeout(30)
        pageLoadr  z	page load)msr   N)r   r   r   r!  r"  r   r#  s     r*   set_page_load_timeoutzWebDriver.set_page_load_timeout  sf    	hLL--
Cl@SVZ@Z<[/\]! 	hLL--eL6ID6PZe/fg	hs   7: 8A54A5c                     | j                  t        j                        d   }|j                  d      dz  |d<   |j                  d      dz  |d<   |j                  d      dz  |d<   t	        di |S )	zGet all the timeouts that have been set on the current session.

        :Usage:
            ::

                driver.timeouts
        :rtype: Timeout
        r   r  r  implicit_waitr(  	page_loadr   r(   )r   r   GET_TIMEOUTSr   r   )r|   timeoutss     r*   r/  zWebDriver.timeouts  ss     << 4 45g>$,LL$<t$C! (Z 84 ?%\\(3d:#(##r)   c                 f    | j                  t        j                  |j                               d   }y)a  Set all timeouts for the session. This will override any previously
        set timeouts.

        :Usage:
            ::
                my_timeouts = Timeouts()
                my_timeouts.implicit_wait = 10
                driver.timeouts = my_timeouts
        r   N)r   r   r   _to_json)r|   r/  _s      r*   r/  zWebDriver.timeouts  s(     LL--x/@/@/BCGLr)   r   c                    t        |t              r2| j                  ||      }|st        d|j                         |d   S |t
        j                  k(  rt
        j                  }d| d}nR|t
        j                  k(  rt
        j                  }d| }n)|t
        j                  k(  rt
        j                  }d| d}| j                  t        j                  ||d      d	   S )
zFind an element given a By strategy and locator.

        :Usage:
            ::

                element = driver.find_element(By.ID, 'foo')

        :rtype: WebElement
        )byr   z%Cannot locate relative element with: r   [id=""]r   [name="usingr   r   )rm   r   find_elementsr   rootr   IDCSS_SELECTOR
CLASS_NAMENAMEr   r   FIND_ELEMENT)r|   r4  r   elementss       r*   find_elementzWebDriver.find_element  s     b*%))Ru)=H,/TUWU\U\T]-^__A;;BE7"%E2== BwKE277]BeWB'E||G00B2OPQXYYr)   c                 R   t        |t              rrdj                  t        j	                  d      dd       }t        j                  |d      j                  d      }d| d}| j                  ||j                               S |t        j                  k(  rt        j                  }d| d	}nR|t        j                  k(  rt        j                  }d| }n)|t        j                  k(  rt        j                  }d
| d	}| j                  t         j"                  ||d      d   xs g S )zFind elements given a By strategy and locator.

        :Usage:
            ::

                elements = driver.find_elements(By.CLASS_NAME, 'foo')

        :rtype: list of WebElement
        r   NzfindElements.jsutf8z/* findElements */return (z).apply(null, arguments);r5  r6  r7  r8  r   )rm   r   joinre   splitpkgutilget_datadecoder   r   r   r<  r=  r>  r?  r   r   FIND_ELEMENTS)r|   r4  r   _pkgraw_functionfind_element_jss         r*   r:  zWebDriver.find_elements  s     b*%88HNN3/45D"++D2CDKKFSL :<.HabO&&

EE;BE7"%E2== BwKE277]BeWB'E ||G11R%3PQRYZ`^``r)   c                     | j                   S )z4Returns the drivers current capabilities being used.)r3   r   s    r*   r-   zWebDriver.capabilities  s     yyr)   c                 @   t        |      j                         j                  d      st        j                  dt
        d       | j                         }	 t        |d      5 }|j                  |       ddd       ~y# 1 sw Y   xY w# t        $ r Y ~yw xY w# ~w xY w)	a  Saves a screenshot of the current window to a PNG image file.
        Returns False if there is any IOError, else returns True. Use full
        paths in your filename.

        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.

        :Usage:
            ::

                driver.get_screenshot_as_file('/Screenshots/foo.png')
        z.pngz^name used for saved screenshot does not match file type. It should end with a `.png` extension   
stacklevelwbNFT)
rp   lowerendswithwarningswarnUserWarningget_screenshot_as_pngopenwriteOSError)r|   filenamepngfs       r*   get_screenshot_as_filez WebDriver.get_screenshot_as_file
  s     8}""$--f5MMp
 ((*	h% 
   		 s<   B
 "A>4B
 >BB
 
	BB BB Bc                 $    | j                  |      S )a  Saves a screenshot of the current window to a PNG image file.
        Returns False if there is any IOError, else returns True. Use full
        paths in your filename.

        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.

        :Usage:
            ::

                driver.save_screenshot('/Screenshots/foo.png')
        )ra  )r|   r^  s     r*   save_screenshotzWebDriver.save_screenshot(  s     **844r)   c                 R    t        | j                         j                  d            S )zGets the screenshot of the current window as a binary data.

        :Usage:
            ::

                driver.get_screenshot_as_png()
        ascii)r   get_screenshot_as_base64encoder   s    r*   rZ  zWebDriver.get_screenshot_as_png8  s#     668??HIIr)   c                 F    | j                  t        j                        d   S )zGets the screenshot of the current window as a base64 encoded string
        which is useful in embedded images in HTML.

        :Usage:
            ::

                driver.get_screenshot_as_base64()
        r   )r   r   
SCREENSHOTr   s    r*   rf  z"WebDriver.get_screenshot_as_base64B  s     ||G../88r)   windowHandlec                 p    | j                  |       | j                  t        |      t        |             y)a$  Sets the width and height of the current window. (window.resizeTo)

        :Args:
         - width: the width in pixels to set the window to
         - height: the height in pixels to set the window to

        :Usage:
            ::

                driver.set_window_size(800,600)
        widthheightN"_check_if_window_handle_is_currentset_window_rectr!  )r|   rm  rn  rj  s       r*   set_window_sizezWebDriver.set_window_sizeM  s-     	//=3u:c&kBr)   c                     | j                  |       | j                         }|j                  dd      r|d   }dD ci c]  }|||   
 c}S c c}w )zGets the width and height of the current window.

        :Usage:
            ::

                driver.get_window_size()
        r   Nrl  )rp  get_window_rectr@   )r|   rj  sizer5   s       r*   get_window_sizezWebDriver.get_window_size\  sS     	//=##%88GT"=D$78q47
888s   Ac                 n    | j                  |       | j                  t        |      t        |            S )a.  Sets the x,y position of the current window. (window.moveTo)

        :Args:
         - x: the x-coordinate in pixels to set the window position
         - y: the y-coordinate in pixels to set the window position

        :Usage:
            ::

                driver.set_window_position(0,0)
        xyro  )r|   ry  rz  rj  s       r*   set_window_positionzWebDriver.set_window_positionm  s0     	//=##c!fA#77r)   c                 v    | j                  |       | j                         }dD ci c]  }|||   
 c}S c c}w )zGets the x,y position of the current window.

        :Usage:
            ::

                driver.get_window_position()
        rx  )rp  rt  )r|   rj  positionr5   s       r*   get_window_positionzWebDriver.get_window_position|  s<     	//='')(2318A;333s   6c                 >    |dk7  rt        j                  dd       yy)z5Warns if the window handle is not equal to `current`.currentz?Only 'current' window is supported for W3C compatible browsers.rQ  rR  N)rW  rX  )r|   rj  s     r*   rp  z,WebDriver._check_if_window_handle_is_current  s    9$MM[hij %r)   c                 F    | j                  t        j                        d   S )zGets the x, y coordinates of the window as well as height and width
        of the current window.

        :Usage:
            ::

                driver.get_window_rect()
        r   )r   r   GET_WINDOW_RECTr   s    r*   rt  zWebDriver.get_window_rect  r   r)   c                 x    |||s|st        d      | j                  t        j                  ||||d      d   S )a  Sets the x, y coordinates of the window as well as height and width
        of the current window. This method is only supported for W3C compatible
        browsers; other browsers should use `set_window_position` and
        `set_window_size`.

        :Usage:
            ::

                driver.set_window_rect(x=10, y=10)
                driver.set_window_rect(width=100, height=200)
                driver.set_window_rect(x=10, y=10, width=100, height=200)
        z'x and y or height and width need values)ry  rz  rm  rn  r   )r   r   r   SET_WINDOW_RECT)r|   ry  rz  rm  rn  s        r*   rq  zWebDriver.set_window_rect  sD     I!)fU*+TUU||G3311u`f5ghipqqr)   c                     | j                   S r   )_file_detectorr   s    r*   rx   zWebDriver.file_detector  s    """r)   c                 b    |st        d      t        |t              st        d      || _        y)a0  Set the file detector to be used when sending keyboard input. By
        default, this is set to a file detector that does nothing.

        see FileDetector
        see LocalFileDetector
        see UselessFileDetector

        :Args:
         - detector: The detector to use. Must not be None.
        z,You may not set a file detector that is nullz+Detector has to be instance of FileDetectorN)r   rm   r   r  )r|   detectors     r*   rx   zWebDriver.file_detector  s1     $%STT(L1$%RSS&r)   c                 F    | j                  t        j                        d   S )zGets the current orientation of the device.

        :Usage:
            ::

                orientation = driver.orientation
        r   )r   r   GET_SCREEN_ORIENTATIONr   s    r*   orientationzWebDriver.orientation  r   r)   c                     ddg}|j                         |v r#| j                  t        j                  d|i       yt	        d      )zSets the current orientation of the device.

        :Args:
         - value: orientation to set it to.

        :Usage:
            ::

                driver.orientation = 'landscape'
        	LANDSCAPEPORTRAITr  z>You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'N)upperr   r   SET_SCREEN_ORIENTATIONr   )r|   r   allowed_valuess      r*   r  zWebDriver.orientation  sA     &z2;;=N*LL77-9OP$%effr)   c                 F    | j                  t        j                        d   S )zGets a list of the available log types. This only works with w3c
        compliant browsers.

        :Usage:
            ::

                driver.log_types
        r   )r   r   GET_AVAILABLE_LOG_TYPESr   s    r*   	log_typeszWebDriver.log_types  s     ||G;;<WEEr)   c                 L    | j                  t        j                  d|i      d   S )a>  Gets the log for a given log type.

        :Args:
         - log_type: type of log that which will be returned

        :Usage:
            ::

                driver.get_log('browser')
                driver.get_log('driver')
                driver.get_log('client')
                driver.get_log('server')
        r   r   )r   r   GET_LOG)r|   log_types     r*   get_logzWebDriver.get_log  s#     ||GOOfh-?@IIr)   c                  K   t                | j                  j                  d      rI| j                  j                  d      }| j                  j                  d      j                  d      d   }n| j	                         \  }}|st        d      t        j                  |      }t        j                  |      4 d {   }|j                  |j                  j                                d {   }|d   j                  }|j                  |      4 d {   }t        |t        |       d d d       d {    d d d       d {    y 7 7 d7 =7 # 1 d {  7  sw Y   +xY w7 "# 1 d {  7  sw Y   y xY ww)Nzse:cdpzse:cdpVersionr   r   z2Unable to find url to connect to from capabilities)r+   r3   r@   rG  _get_cdp_detailsr   r'   import_devtoolsopen_cdpr   targetget_targets	target_idopen_sessionr   )r|   ws_urlversiondevtoolsconntargetsr  sessions           r*   bidi_connectionzWebDriver.bidi_connection  s?     	99=="YY]]8,FiimmO4::3?BG"335OGV$%YZZ&&w/<<' 	= 	=4 LL)D)D)FGGG
,,I((3 = =w$Wc8<<= =	= 	= 	=G= = = = =	= 	= 	= 	=s   B=F?E F-E20E1(E2EE2E2E2=E>E2FE0FE2E2E2E-	!E$"E-	)E20F2F8E;9F Fc                    dd l }dd l}|j                         }d}| j                  j	                  d      dk(  r+| j                  j	                  d      j	                  d      }nf| j                  j	                  d      dk(  r+| j                  j	                  d      j	                  d      }nd	}| j                  j	                  d
      }|j                  dd| d      }|j                  |j                        }|j	                  d      }|j	                  d      }	dd l}
|rd}||	fS |
j                  d|      j                  d      }||	fS )Nr   Fr>   chromezgoog:chromeOptionsdebuggerAddressMicrosoftEdgezms:edgeOptionsTzmoz:debuggerAddressr   zhttp://z/json/versionBrowserwebSocketDebuggerUrlU   z
.*/(\d+)\.r   )jsonurllib3PoolManagerr3   r@   requestloadsdataresearchgroup)r|   r  r  http_firefoxdebugger_addressresr  browser_versionwebsocket_urlr  r  s               r*   r  zWebDriver._get_cdp_details  s*   ""$99=='83#yy}}-ABFFGXYYY]]=)_<#yy}}-=>BBCTUH#yy}}-BCll5G,<+=]"KLzz#((#((9-!78 G %% ii?EEaHG%%r)   c                 p    | j                  t        j                  |j                               d   | _        y)z4Adds a virtual authenticator with the given options.r   N)r   r   ADD_VIRTUAL_AUTHENTICATORr   ry   )r|   rP   s     r*   add_virtual_authenticatorz#WebDriver.add_virtual_authenticator0  s+    !%g.O.OQXQ`Q`Qb!cdk!lr)   c                     | j                   S )z,Returns the id of the virtual authenticator.)ry   r   s    r*   virtual_authenticator_idz"WebDriver.virtual_authenticator_id4  s     %%%r)   c                 j    | j                  t        j                  d| j                  i       d| _        y)zRemoves a previously added virtual authenticator.

        The authenticator is no longer valid after removal, so no
        methods may be called.
        authenticatorIdN)r   r   REMOVE_VIRTUAL_AUTHENTICATORry   r   s    r*   remove_virtual_authenticatorz&WebDriver.remove_virtual_authenticator9  s,     	W99<MtOeOe;fg!%r)   
credentialc                     | j                  t        j                  i |j                         d| j                  i       y)z,Injects a credential into the authenticator.r  N)r   r   ADD_CREDENTIALr   ry   )r|   r  s     r*   add_credentialzWebDriver.add_credentialC  s6     	W++-p
0B0B0D-pFWY]YoYo-pqr)   c                     | j                  t        j                  d| j                  i      }|d   D cg c]  }t	        j
                  |       c}S c c}w )z;Returns the list of credentials owned by the authenticator.r  r   )r   r   GET_CREDENTIALSry   r   	from_dict)r|   credential_datar  s      r*   get_credentialszWebDriver.get_credentialsH  sL     ,,w'>'>ARTXTjTj@klCRSZC[\Z
$$Z0\\\s   Acredential_idc                     t        |t              rt        |      j                         }| j	                  t
        j                  || j                  d       y)z,Removes a credential from the authenticator.)credentialIdr  N)rm   	bytearrayr   rJ  r   r   REMOVE_CREDENTIALry   )r|   r  s     r*   remove_credentialzWebDriver.remove_credentialN  sD     mY/-m<CCEM%%Z^ZpZp'q	
r)   c                 \    | j                  t        j                  d| j                  i       y)z/Removes all credentials from the authenticator.r  N)r   r   REMOVE_ALL_CREDENTIALSry   r   s    r*   remove_all_credentialsz WebDriver.remove_all_credentialsY  s$     	W336GI_I_5`ar)   verifiedc                 ^    | j                  t        j                  | j                  |d       y)zSets whether the authenticator will simulate success or fail on user
        verification.

        verified: True if the authenticator will pass user verification, False otherwise.
        )r  isUserVerifiedN)r   r   SET_USER_VERIFIEDry   )r|   r  s     r*   set_user_verifiedzWebDriver.set_user_verified^  s$     	W..DDZDZnv0wxr)   c                 ~    d| j                   vrt        d      | j                  t        j                        d   d   S )z]Retrieves the downloadable files as a map of file names and their
        corresponding URLs.se:downloadsEnabledCYou must enable downloads in order to work with downloadable files.r   names)r-   r   r   r   GET_DOWNLOADABLE_FILESr   s    r*   get_downloadable_filesz WebDriver.get_downloadable_filesg  s=     !(9(99$%jkk||G::;GDWMMr)   	file_nametarget_directoryc                 0   d| j                   vrt        d      t        j                  j	                  |      st        j
                  |       | j                  t        j                  d|i      d   d   }t        j                  j                  ||      }t        |d      5 }|j                  t        j                  |             ddd       t        j                  |d      5 }|j!                  |       ddd       y# 1 sw Y   :xY w# 1 sw Y   yxY w)	zDownloads a file with the specified file name to the target
        directory.

        file_name: The name of the file to download.
        target_directory: The path to the directory to save the downloaded file.
        r  r  r   r   contentsrT  Nr)r-   r   ospathexistsmakedirsr   r   DOWNLOAD_FILErF  r[  r\  base64r   zipfileZipFile
extractall)r|   r  r  r  target_filefilezip_refs          r*   download_filezWebDriver.download_fileo  s     !(9(99$%jkkww~~./KK()<< 5 5	7JKGTU_`ggll#3Y?+t$ 	3JJv''12	3 __[#. 	1'/0	1 	1	3 	3	1 	1s   "%D %D D	Dc                 t    d| j                   vrt        d      | j                  t        j                         y)zDeletes all downloadable files.r  r  N)r-   r   r   r   DELETE_DOWNLOADABLE_FILESr   s    r*   delete_downloadable_filesz#WebDriver.delete_downloadable_files  s.     (9(99$%jkkW667r)   )zhttp://127.0.0.1:4444TNNr   )rQ   N)r  )NNNN)rre   rf   rg   rh   r%   r   r#   r   r   r   r
   r}   r   r   typingr   TypeBaseExceptiontypesTracebackTyper   r   r   propertyr    r   rp   r   rz   r   r   r{   r   r   r   r   r@   r   r"   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r   r  r  r	   r  r  r  r  r"  r$  r&  r*  r   r/  setterr   r<  rB  r:  r-   boolra  rc  rq   rZ  rf  rr  rv  r{  r~  rp  rt  rq  r   rx   r  r  r  r   r  r  r   r  r  r   r  r   r  r  r  r  r  r  r  r  r  r(   r)   r*   rk   rk      s   
 " O 19=,)
 {D$556,) 
,)\`//&++m"<= __]+ ??5#6#67	 3 36    
Lc 
L 
L
1$ 
14 
17S 7Z 7Kc K4 K4 K60s 0t 0 @s @ @# #) #k	 kd k)DI )Z.Z3 Z& >S > > >S > >$* Ls L L ES	 E E$
0.
B(> 
B# 
B 8  (&)&	>T$Z 	>&//&++"> <1F,ZE Zd Z Xu X Xh% hD h" $( $ $ __
M 
M !eeD ZHSM ZZ Z8  "uuT aXc] ad:FV a< d  $ <54 5 Ju J	9# 	9C3 Ct C9C 9 9"8c 8$ 84T 4ks kt k
	> 	>r$ r& #| # # ' '" E E g g" 	F 	FJ  = =&&@m1L mQU m &# & & $& $& $r r r $r $]j!1 ] $]
 $
uS)^/D 
 
 $
 $b $b $y$ y4 y $yN N1s 1c 1d 1,8r)   rk   )F)Frh   r  r  r0   r  rH  r  r  rW  r  abcr   r   r   r   r   	importlibr   r	   r
   r   r   selenium.common.exceptionsr   r   r   r   r   selenium.webdriver.common.byr   !selenium.webdriver.common.optionsr   ,selenium.webdriver.common.print_page_optionsr   "selenium.webdriver.common.timeoutsr   /selenium.webdriver.common.virtual_authenticatorr   r   r   +selenium.webdriver.support.relative_locatorr   r  r   r   r   errorhandlerr   rx   r   r   r   r    remote_connectionr!   r   r"   
shadowrootr#   r   r$   
webelementr%   r'   r+   r7   rO   rb   rd   rk   r(   r)   r*   <module>r     s   " $    	        $ * % #     ? : < = 9 + 9 E 7 F W C +  & ' ,  / ! "  "
BO&	]D- $ Dg v8 v8r)   