
    >iD                    &   d dl mZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZmZ d dlmZ d dlm Z m!Z! erd dl"m#Z#  ejH                  e%      Z&e G d d             Z' G d d      Z(y)    )annotationsN)	dataclass)TYPE_CHECKINGIterableOptional)DomCommandsPageCommandsRuntimeCommandsTargetCommands)ConnectionHandler)InvalidIFrame)DescribeNodeResponseGetFrameOwnerResponse)Node)CreateIsolatedWorldResponseGetFrameTreeResponse)Frame	FrameTree)EvaluateResponse)AttachToTargetResponseGetTargetsResponse)
WebElementc                  b    e Zd ZU dZded<   dZded<   dZded<   dZded	<   dZd
ed<   dZ	ded<   y)IFrameContextz*Context information for an iframe element.strframe_idNOptional[str]document_urlOptional[int]execution_context_iddocument_object_idOptional[ConnectionHandler]session_handler
session_id)
__name__
__module____qualname____doc____annotations__r   r    r!   r#   r$        `/var/www/python/tenders/Scrapper/venv/lib/python3.12/site-packages/pydoll/interactions/iframe.pyr   r      s?    4M"&L-&*.-.(,,37O07 $J$r+   r   c                  l   e Zd ZdZddZddZddZ	 	 	 	 	 	 ddZe	 	 	 	 dd       Z		 	 	 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 dd	       Zedd
       Ze	 	 	 	 	 	 	 	 dd       Z	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 ddZedd       Ze	 	 	 	 	 	 	 	 d d       Z	 	 	 	 	 	 d!dZy)"IFrameContextResolverz'Resolves iframe context for WebElement.c                    || _         y )N)_element)selfelements     r,   __init__zIFrameContextResolver.__init__&   s	    r+   c                ,  K   | j                         \  }}| j                  ||       d{   }| j                  |      \  }}}}|s!|| j                  ||||       d{   \  }}| j	                  ||||||       d{   \  }}	}}|st        d      t        ||      }
|r|	r||
_        |	|
_        |xs |}|	xs |}| j                  |||       d{   }||
_
        | j                  ||
       d{   }||
_        |
S 7 7 7 7 47 w)a+  
        Resolve and return iframe context.

        Returns:
            IFrameContext with frame_id, document_url, execution_context_id,
            document_object_id and session info for OOPIF targets.

        Raises:
            InvalidIFrame: If unable to resolve the iframe context.
        N)current_document_urlbase_handlerbase_session_idz0Unable to resolve frameId for the iframe element)r   r   )_get_base_session_describe_element_node_extract_frame_metadata_resolve_frame_by_owner_resolve_oopif_if_neededr   r   r#   r$    _create_isolated_world_for_framer    _get_document_object_idr!   )r1   r6   r7   	node_infor   r   content_frame_idbackend_node_idr#   r$   contexteffective_handlereffective_session_idr    r!   s                  r,   resolvezIFrameContextResolver.resolve)   sm     )-(>(>(@%o55lOTT	DHD`D`E
A, 0/ O7+/+G+Go, &"Hl EIDaDa!-%+ Eb E
 ?
;X|  RSSMz&5G#!+G+;|)<_%)%J%J')=&
  
 (<$#'#?#?@TV]#^^%7"O U&?
* 

 _sX   )DD
5D!D"!DDADD D<D=DDDDDc                    t        | j                  dd      xs | j                  j                  }t        | j                  dd      }||fS )z?Return the default handler and session id for routing commands._routing_session_handlerN_routing_session_id)getattrr0   _connection_handler)r1   handlerr$   s      r,   r8   z'IFrameContextResolver._get_base_session^   sL     DMM#=tD 1}}00 	 T]],A4H

""r+   c                   K   t        j                  | j                  j                        }|r||d<   |j	                  |       d{   }d|v ri S |j                  di       j                  di       S 7 ,w)a  Describe the iframe element using the given handler/session.

        This bypasses ``_resolve_routing()`` which, after a previous
        resolution, may return the iframe *content* session instead of
        the parent session where the element actually lives.
        )	object_id	sessionIdNerrorresultnode)r   describe_noder0   
_object_idexecute_commandget)r1   rK   r$   commandresponses        r,   r9   z,IFrameContextResolver._describe_element_nodeg   ss      ++dmm6N6NO#-GK /6/F/Fw/O)OhI||Hb)--fb99 *Ps   AA7A5	-A7c                2   | j                  d      xs i }| j                  d      }| j                  d      }|j                  d      }|j                  d      xs7 |j                  d      xs$ | j                  d      xs | j                  d      }||||fS )a  Extract iframe-related metadata from DOM node info.

        Returns:
            Tuple of (frame_id, document_url, content_frame_id, backend_node_id).
            ``content_frame_id`` is the frame ID of the frame *created* by the
            ``<iframe>`` element (``node_info['frameId']`` on frame-owner
            elements).  For same-origin iframes it equals
            ``contentDocument.frameId``; for OOPIFs ``contentDocument`` is
            absent but ``content_frame_id`` is still set by the browser.
        contentDocumentframeIdbackendNodeIddocumentURLbaseURL)rU   )r?   content_documentr@   rA   r   r   s         r,   r:   z-IFrameContextResolver._extract_frame_metadataz   s     %==):;Ar$==3#--8#''	2  / (##I.(}}]+( }}Y'	 	 '7HHr+   c                f   K   | j                  |||       d{   \  }}|sd|fS ||xs |fS 7 w)z;Resolve frame id and URL by matching owner backend_node_id.N)_find_frame_by_owner)r1   r6   r7   rA   r5   owner_frame_id	owner_urls          r,   r;   z-IFrameContextResolver._resolve_frame_by_owner   sP      +/*C*C/?+
 %
!	 ---y@,@@@%
s   1/1c                  K   | j                  ||       d{   }| j                  |      D ]L  }|j                  dd      }|s| j                  |||       d{   }||k(  s9||j                  d      fc S  y7 e7 #w)z-Find frame by matching owner backend_node_id.Nid urlNN)_get_frame_tree_for_walk_framesrU   _owner_backend_for)r1   rK   r$   rA   
frame_tree
frame_nodecandidate_frame_idowner_backend_ids           r,   r`   z*IFrameContextResolver._find_frame_by_owner   s       33GZHH
++J7 	AJ!+b!9%%)%<%<%7&    ?2):>>%+@@@	A  I
 s(   BA>ABB 	B'B Bc                   K   t        j                         }|r||d<   | j                  |       d{   }|d   d   S 7 w)z4Get Page frame tree for the given connection/target.rN   NrP   	frameTree)r	   get_frame_treerT   )rK   r$   rV   rW   s       r,   rh   z)IFrameContextResolver._get_frame_tree_for   sL      --/#-GK /6/F/Fw/O)O!+.. *Ps   0A?Ac                    | sg S | d   g}| j                  dg       xs g D ]&  }|j                  t        j                  |             ( |D cg c]  }|s|	 c}S c c}w )zARecursively traverse FrameTree and collect all frame descriptors.framechildFrames)rU   extendr.   ri   )treeframeschild_framerl   s       r,   ri   z"IFrameContextResolver._walk_frames   sf     I#G}o88M26<" 	KKMM/<<[IJ	K-3Bzz
BBBs   AAc                   K   t        j                  |      }|r||d<   | j                  |       d{   }|j                  di       j                  d      S 7 %w)z?Get backendNodeId of the DOM element that owns the given frame.)r   rN   NrP   r[   )r   get_frame_ownerrT   rU   )rK   r$   r   rV   rW   s        r,   rj   z(IFrameContextResolver._owner_backend_for   sZ      --x@#-GK 070G0G0P*P||Hb)--o>> +Qs   2AA&ANc                   K   |r|r|dd||fS | j                  ||||       d{   \  }}}	}
|r|r|
r|||	xs ||
xs |fS dd|xs |	|xs |
fS 7 +w)z&Resolve OOPIF and routing when needed.N)_resolve_oopif_by_parent)r1   current_frame_idr@   rA   r5   r6   r7   r#   r$   resolved_frame_idresolved_urls              r,   r<   z.IFrameContextResolver._resolve_oopif_if_needed   s       $49P/1EEE //o|_
 
	

 zl!5%54 4	  1 1 0L	
 	

s   $AA,Ac           	     D  K   t        | j                  j                  j                        }|j	                  t        j                                d{   }|j                  di       j                  dg       }|xs | j                  j                  }|}	|D 
cg c]+  }
|
j                  d      dv r|
j                  d      |k(  r|
- }}
t        |      dk(  }|D ]  }|j	                  t        j                  |d	   d
             d{   }|j                  di       j                  d      }|sX| j                  ||       d{   }|xs i j                  di       }|j                  dd      }|r|r|||||j                  d      fc S |s|| j                  ||	|       d{   }||k(  s||||j                  d      fc S  |D ]0  }
|
j                  d      dvr|j	                  t        j                  |
j                  d	d      d
             d{   }|j                  di       j                  d      }|sz| j                  ||       d{   }|xs i j                  di       }|j                  dd      }|r||k(  r||||j                  d      fc S |r9|7| j                  ||	|       d{   }||k(  r||||j                  d      fc S | j                  ||      }|s+|||dfc S  y7 c c}
w 7 7 7 h7 7 7 Uw)as  Resolve out-of-process iframe using content frame id.

        ``content_frame_id`` is the frame ID of the frame *created* by the
        ``<iframe>`` element (obtained from ``DOM.describeNode``'s
        ``node.frameId``).  For OOPIF targets the root frame of the target
        shares this ID, so we can match directly without needing
        ``DOM.getFrameOwner``.

        When a direct frame-ID match is not possible (e.g. nested sub-frames
        inside the OOPIF), the method falls back to ``DOM.getFrameOwner``
        using the routing handler/session that has DOM visibility into the
        parent context.
        )connection_portNrP   targetInfostype>   pageiframeparentFrameId   targetIdT)	target_idflattenrN   rs   rd   re   rf   )NNNN)r   r0   rJ   _connection_portrT   r   get_targetsrU   lenattach_to_targetrh   rj   _find_child_by_parent)r1   r@   rA   r6   r7   browser_handlertargets_responsetarget_infosowner_handlerowner_session_idtarget_infodirect_childrenis_single_childchild_targetattach_responseattached_session_idrk   
root_frameroot_frame_idrn   child_frame_ids                        r,   r|   z.IFrameContextResolver._resolve_oopif_by_parent   s    ( , MM==NN
 6E5T5T&&(6
 0
 (++Hb9==mRP %I(I(I*  ,
v&*<<04DD 
 
 o.!3+ 	L<K<[<[//,z:R\`a= 7O #2"5"5h"C"G"G"T&#77I\]]J$*//<J&NN44M=_5L#'!NN5)	  !<)-)@)@!#3]* $  $6'+%"u-	 3	@ ( *	RKv&.@@$3$C$C//)ooj"=t% O
 #2"5"5h"C"G"G"T&#77I\]]J$*//<J&NN44M 2B!B#'!NN5)	  !<)-)@)@!#3]* $  $6'+%"u-	  "77
DTUN&(;^TQQU*	RX &A0

7 ^$ ^"$s   AL L	AL 0L
AL L=L 	L
AL L L /L0	L :A+L %L&=L #L$A$L L	4L ?L L L L L L L c                    | sy| j                  dg       xs g D ]W  }|j                  di       }|j                  d      |k(  r|j                  d      c S t        j                  ||      }|sU|c S  y)z;Find id of child frame whose parentId equals the given one.Nrt   rs   parentIdrd   )rU   r.   r   )rv   	parent_idchildrx   founds        r,   r   z+IFrameContextResolver._find_child_by_parentw  sx     XXmR06B 	E))GR0Kz*i7"t,,)??yQE	 r+   c                   K   t        j                  | d|  d      }|r||d<   |j                  |       d{   }|j                  di       j                  d      }|st	        d      |S 7 4w)	z*Create isolated world for the given frame.zpydoll::iframe::T)r   
world_namegrant_universal_accessrN   NrP   executionContextIdz*Unable to create isolated world for iframe)r	   create_isolated_worldrT   rU   r   )r   rK   r$   create_commandcreate_responser    s         r,   r=   z6IFrameContextResolver._create_isolated_world_for_frame  s      &;;)(4#'

 *4N;'=D=T=TUc=d7d.228R@DDEYZ# LMM##	 8es   7A0A.5A0c                p  K   t        j                  d|      }|j                  r|j                  |d<   |j                  xs | j                  j
                  }|j                  |       d{   }|j                  di       j                  di       }|j                  d      }|st        d      |S 7 Fw)z9Get document.documentElement object id in iframe context.zdocument.documentElement)
expression
context_idrN   NrP   objectIdz.Unable to obtain document reference for iframe)	r
   evaluater$   r#   r0   rJ   rT   rU   r   )r1   r    rB   evaluate_commandrK   evaluate_responseresult_objectr!   s           r,   r>   z-IFrameContextResolver._get_document_object_id  s      +331+
 ,3,>,>[)))NT]]-N-N4;4K4KL\4].])--h;??"M*..z:! PQQ!! /^s   A+B6-B4.AB6)r2   r   )returnr   )r   z'tuple[ConnectionHandler, Optional[str]])rK   r   r$   r   r   r   )r?   r   r   zAtuple[Optional[str], Optional[str], Optional[str], Optional[int]])
r6   r   r7   r   rA   intr5   r   r   #tuple[Optional[str], Optional[str]])rK   r   r$   r   rA   r   r   r   )rK   r   r$   r   r   r   )rv   r   r   zIterable[Frame])rK   r   r$   r   r   r   r   r   rg   )r}   r   r@   r   rA   r   r5   r   r6   r"   r7   r   r   Otuple[Optional[ConnectionHandler], Optional[str], Optional[str], Optional[str]])
r@   r   rA   r   r6   r"   r7   r   r   r   )rv   r   r   r   r   r   )r   r   rK   r   r$   r   r   r   )r    r   rB   r   r   r   )r%   r&   r'   r(   r3   rE   r8   r9   staticmethodr:   r;   r`   rh   ri   rj   r<   r|   r   r=   r>   r*   r+   r,   r.   r.   #   sE   1 3j#:": ": 
	:& II	JI I2A'A 'A 	A
 ,A 
-A" " 	
 
-& 	/"	/!	/ 
	/ 	/ C C 
?"
?!
? 
? 
	
? 
?$ 59)-#
'#
 (#
 '	#

 ,#
 2#
 '#
 
Y#
R 59)-w&w& 'w& 2	w&
 'w& 
Yw&r   $$"$ "$ 
	$ $&"!" " 
	"r+   r.   ))
__future__r   loggingdataclassesr   typingr   r   r   pydoll.commandsr   r	   r
   r   pydoll.connectionr   pydoll.exceptionsr   pydoll.protocol.dom.methodsr   r   pydoll.protocol.dom.typesr   pydoll.protocol.page.methodsr   r   pydoll.protocol.page.typesr   r   pydoll.protocol.runtime.methodsr   pydoll.protocol.target.methodsr   r   pydoll.elements.web_elementr   	getLoggerr%   loggerr   r.   r*   r+   r,   <module>r      so    "  ! 4 4 V V / + S * Z 7 < U6			8	$ % % %J" J"r+   