o
    衡i                     @   s^   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	 G dd dZ
dS )    N)SeleniumManagerc                       s  e Zd ZdZ								d'ddZedd	 Zejd
d	 Zedd Zedd Z	e	jdd Z	edd Z
e
j fddZ
edd Zej fddZedd Zejdd Zedd Zejdd Zedd Zejdd Zd(dd Zd)d!d"Zd#d$ Zd%d& Z  ZS )*Servera  Manage a Selenium Grid (Remote) Server in standalone mode.

    This class contains functionality for downloading the server and starting/stopping it.

    For more information on Selenium Grid, see:
        - https://www.selenium.dev/documentation/grid/getting_started/

    Args:
        host: Hostname or IP address to bind to (determined automatically if not specified).
        port: Port to listen on (4444 if not specified).
        path: Path/filename of existing server .jar file (Selenium Manager is used if not specified).
        version: Version of server to download (latest version if not specified).
        log_level: Logging level to control logging output ("INFO" if not specified).
            Available levels: "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST".
        env: Mapping that defines the environment variables for the server process.
        java_path: Path to the java executable to run the server.
    N\  INFO
   c	           	      C   sJ   |r|rt d|| _|| _|| _|| _|| _|| _|| _|| _d | _	d S )NzCNot allowed to specify a version when using an existing server path)
	TypeErrorhostportpathversion	log_levelenv	java_pathstartup_timeoutprocess)	selfr   r	   r
   r   r   r   r   r    r   q/var/www/html/asbeauty/laura_geller_scraping/env/lib/python3.10/site-packages/selenium/webdriver/remote/server.py__init__1   s   
zServer.__init__c                 C      | j S N)_startup_timeoutr   r   r   r   r   I      zServer.startup_timeoutc                 C   s   t || _d S r   )intr   )r   timeoutr   r   r   r   M   s   c                 C   s(   | j d ur| j nd}d| d| j dS )N	localhostzhttp://:z/status)r   r	   )r   r   r   r   r   
status_urlQ   s   zServer.status_urlc                 C   r   r   )_pathr   r   r   r   r
   V   r   zServer.pathc                 C   (   |rt j|std| || _d S )Nz"Can't find server .jar located at )osr
   existsOSErrorr   )r   r
   r   r   r   r
   Z      
c                 C   r   r   )_portr   r   r   r   r	   `   r   zServer.portc                    s`   zt |}W n ty   t j d| dw d|  kr&dks+td td|| _d S )Nz".__init__() got an invalid port: ''r   i  zport must be 0-65535)r   
ValueErrorr   __name__r%   )r   r	   	__class__r   r   r	   d   s   
c                 C   r   r   )_versionr   r   r   r   r   n   r   zServer.versionc                    s4   |rt dt|st j d| d|| _d S )Nz^\d+\.\d+\.\d+$z%.__init__() got an invalid version: 'r&   )rematchstrr   r(   r+   )r   r   r)   r   r   r   r   s   
c                 C   r   r   )
_log_levelr   r   r   r   r   y   r   zServer.log_levelc                 C   s*   d}||vrt dd| || _d S )N)SEVEREWARNINGr   CONFIGFINEFINERFINESTzlog_level must be one of: z, )r   joinr/   )r   r   levelsr   r   r   r   }   s   
c                 C   r   r   )_envr   r   r   r   r      r   z
Server.envc                 C   s(   |d urt |tjjstd|| _d S )Nz.env must be a mapping of environment variables)
isinstancecollectionsabcMappingr   r8   )r   r   r   r   r   r      s   
c                 C   r   r   )
_java_pathr   r   r   r   r      r   zServer.java_pathc                 C   r    )Nz&Can't find java executable located at )r!   r
   r"   r#   r=   )r   r   r   r   r   r      r$   c                 C   sb   t   }t   | |k r/z
tj| j W dS  tjjy&   t d Y nw t   | |k sdS )NTg?F)timeurllibrequesturlopenr   errorURLErrorsleep)r   r   startr   r   r   _wait_for_server   s   zServer._wait_for_serverc                 C   s(   dg}|dur| | t |d S )zqDownload the server if it doesn't already exist.

        Latest version is downloaded unless specified.
        z--gridNdriver_path)appendr   binary_paths)r   r   argsr   r   r   download_if_needed   s   
zServer.download_if_neededc                 C   sH  | j du r| | jn| j }| jptd}|du rtd|d|ddt| jd| j	dd	d
d	g}| j
dur=|d| j
g | j
durE| j
nd}z(ttjtj}||| jf W d   n1 scw   Y  td| j  ty   td tj|| jd| _td| jj  | j| jdstd| j td Y | jS w )zStart the server.

        Selenium Manager will detect the server location and download it if necessary,
        unless an existing server path was specified.
        NjavazJCan't find java on system PATH. JRE is required to run the Selenium serverz-jar
standalonez--portz--log-levelz--selenium-managertruez--enable-managed-downloadsz--hostr   zDSelenium server is already running, or something else is using port zStarting Selenium server...)r   z$Selenium server running as process: )r   z)Timed out waiting for Selenium server at zSelenium server is ready)r
   rK   r   r   shutilwhichr#   r.   r	   r   r   extendsocketAF_INETSOCK_STREAMconnectConnectionErrorConnectionRefusedErrorprint
subprocessPopenr   r   pidrF   r   TimeoutErrorr   )r   r
   r   commandr   sockr   r   r   rE      sD   

zServer.startc                 C   sF   | j du r	td| j  du r| j   | j   d| _ td dS )zStop the server.NzSelenium server isn't runningz#Selenium server has been terminated)r   RuntimeErrorpoll	terminatewaitrX   r   r   r   r   stop   s   


zServer.stop)Nr   NNr   NNr   )r   r   )r(   
__module____qualname____doc__r   propertyr   setterr   r
   r	   r   r   r   r   rF   rK   rE   rc   __classcell__r   r   r)   r   r      sZ    






	










,r   )r:   r!   r,   rO   rR   rY   r>   r?   *selenium.webdriver.common.selenium_managerr   r   r   r   r   r   <module>   s   