B
     h              
   @   s  d 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mZ ddlmZmZmZ ddlmZ ddlmZ yeZW n ek
r   Y nX e dddddddddg	Z!ddddZ"dd Z#G dd de$Z%dS )zThe WebDriver implementation.    N)contextmanager   )Command)
WebElement)RemoteConnection)ErrorHandler)SwitchTo)Mobile)FileDetectorLocalFileDetector)InvalidArgumentExceptionWebDriverExceptionNoSuchCookieException)By)ApplicationCacheacceptInsecureCertsbrowserNamebrowserVersionZplatformNameZpageLoadStrategyproxyZsetWindowRectZtimeoutsZunhandledPromptBehavior)ZacceptSslCertsversionplatformc             C   s   t | } | d}i }| drH| d drH| d d  | d d< xT|  D ]H\}}|r|tkr|dkrv| n||t| < |tksd|krR|||< qRW |r|di }d|krt |}||d< ||d< i g|d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.
    firefox_profiler   Z	proxyTyper   :zmoz:firefoxOptionsprofile)Z
firstMatchZalwaysMatch)copydeepcopygetloweritems_OSS_W3C_CONVERSION_W3C_CAPABILITY_NAMES)Zcapsr   Zalways_matchkvZmoz_optsZnew_opts r#   `C:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_packages\selenium\webdriver\remote\webdriver.py_make_w3c_caps?   s"    


r%   c               @   s  e Zd ZdZeZdddZdd Zd	d
 Zdd Z	e
dd Zedd Zedd Zdd Zdd ZdddZdd Zdd Zdd Zddd Zd!d" Zed#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Z d7d8 Z!d9d: Z"d;d< Z#d=d> Z$d?d@ Z%dAdB Z&dCdD Z'dEdF Z(dGdH Z)edIdJ Z*edKdL Z+dMdN Z,dOdP Z-edQdR Z.edSdT Z/dUdV Z0dWdX Z1dYdZ Z2ed[d\ Z3d]d^ Z4d_d` Z5dadb Z6dcdd Z7dedf Z8dgdh Z9didj Z:dkdl Z;dmdn Z<dodp Z=dqdr Z>dsdt Z?dudv Z@dwdx ZAdydz ZBd{d| ZCeDjEdfd}d~ZFeDjEdfddZGedd ZHdd ZIdd ZJdd ZKdd ZLdddZMdddZNdddZOdddZPdd ZQdddZRedd ZSeSjTdd ZSedd ZUeUjTdd ZUedd ZVedd ZWdd ZXdS )	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://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

    :Attributes:
     - session_id - String ID of the browser session started and controlled by this WebDriver.
     - capabilities - Dictionaty of effective capabilities of this browser session as returned
         by the remote server. See https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
     - command_executor - remote_connection.RemoteConnection object used to execute commands.
     - error_handler - errorhandler.ErrorHandler object used to handle errors.
    http://127.0.0.1:4444/wd/hubNFc       	      C   s   i }|dk	r|  }|dk	r:t|ts0tdn
|| |dk	r\tjdtdd || || _	t
| j	tks|t| j	trt||d| _	d| _d| _i | _t | _|   |dk	rtjdtdd | || t| | _t| | _|pt | _dS )	aY  
        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'.
         - desired_capabilities - A dictionary of capabilities to request when
             starting the browser session. Required parameter.
         - browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object.
             Only used if Firefox is requested. Optional.
         - proxy - A selenium.webdriver.common.proxy.Proxy object. The browser session will
             be started with given proxy settings, if possible. Optional.
         - keep_alive - Whether to configure remote_connection.RemoteConnection to use
             HTTP keep-alive. Defaults to False.
         - 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
        Nz)Desired Capabilities must be a dictionaryz&Please use FirefoxOptions to set proxy   )
stacklevel)
keep_aliveTz0Please use FirefoxOptions to set browser profile)to_capabilities
isinstancedictr   updatewarningswarnDeprecationWarningadd_to_capabilitiescommand_executortypebytesstrr   
_is_remote
session_idcapabilitiesr   error_handlerstart_clientstart_sessionr   
_switch_tor	   _mobiler   file_detector)	selfr3   desired_capabilitiesbrowser_profiler   r*   r?   optionsr9   r#   r#   r$   __init__q   s4    







zWebDriver.__init__c             C   s   d t| | jS )Nz-<{0.__module__}.{0.__name__} (session="{1}")>)formatr4   r8   )r@   r#   r#   r$   __repr__   s    zWebDriver.__repr__c             C   s   | S )Nr#   )r@   r#   r#   r$   	__enter__   s    zWebDriver.__enter__c             G   s   |    d S )N)quit)r@   argsr#   r#   r$   __exit__   s    zWebDriver.__exit__c             o   sB   d}t | j|s"| j}|||| _z
dV  W d|dk	r<|| _X dS )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)r,   r?   )r@   Zfile_detector_classrI   kwargsZlast_detectorr#   r#   r$   file_detector_context   s    
zWebDriver.file_detector_contextc             C   s   | j S )N)r>   )r@   r#   r#   r$   mobile   s    zWebDriver.mobilec             C   s    d| j kr| j d S tddS )zvReturns the name of the underlying browser for this instance.

        :Usage:
            name = driver.name
        r   z1browserName not specified in session capabilitiesN)r9   KeyError)r@   r#   r#   r$   name   s    

zWebDriver.namec             C   s   dS )z
        Called before starting a new session. This method may be overridden
        to define custom startup behavior.
        Nr#   )r@   r#   r#   r$   r;      s    zWebDriver.start_clientc             C   s   dS )z
        Called after executing a quit command. This method may be overridden
        to define custom shutdown behavior.
        Nr#   )r@   r#   r#   r$   stop_client   s    zWebDriver.stop_clientc             C   s   t |tstd|r>d|kr.|j|d d< n|d|ji t|}||d}| tj|}d|krn|d }|d | _	|
d| _| jdkr|
d	| _|
d
dk| _| j| j_dS )a  
        Creates a new session with the desired capabilities.

        :Args:
         - browser_name - The name of the browser to request.
         - version - Which browser version to request.
         - platform - Which platform to request the browser on.
         - javascript_enabled - Whether the new session should support JavaScript.
         - browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested.
        z!Capabilities must be a dictionaryzmoz:firefoxOptionsr   r   )r9   ZdesiredCapabilities	sessionIdvalueNr9   status)r,   r-   r   encodedr.   r%   executer   ZNEW_SESSIONr8   r   r9   w3cr3   )r@   r9   rB   Zw3c_caps
parametersresponser#   r#   r$   r<      s$    


zWebDriver.start_sessionc                sx   t |tr6i }x"| D ]\}} |||< qW |S t | jrP|j|jdS t |trpt fdd|D S |S d S )N)ELEMENTz#element-6066-11e4-a52e-4f735466cecfc             3   s   | ]}  |V  qd S )N)_wrap_value).0item)r@   r#   r$   	<genexpr>  s    z(WebDriver._wrap_value.<locals>.<genexpr>)r,   r-   r   rZ   _web_element_clsidlist)r@   rR   Z	convertedkeyvalr#   )r@   r$   rZ     s    

zWebDriver._wrap_valuec             C   s   | j | || jdS )z6Creates a web element with the specified `element_id`.)rV   )r^   rV   )r@   Z
element_idr#   r#   r$   create_web_element  s    zWebDriver.create_web_elementc                s   t |trrd|ksd|krH|dd }|r8 |d S  |d S qx"| D ]\}} |||< qRW |S n$t |trt fdd|D S |S d S )NrY   z#element-6066-11e4-a52e-4f735466cecfc             3   s   | ]}  |V  qd S )N)_unwrap_value)r[   r\   )r@   r#   r$   r]   )  s    z*WebDriver._unwrap_value.<locals>.<genexpr>)r,   r-   r   rc   r   rd   r`   )r@   rR   Z
wrapped_idra   rb   r#   )r@   r$   rd     s    

zWebDriver._unwrap_valuec             C   s|   | j dk	r,|sd| j i}nd|kr,| j |d< | |}| j||}|rn| j| | |dd|d< |S dd| j dS )aQ  
        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.
        NrQ   rR   r   )ZsuccessrR   rQ   )r8   rZ   r3   rU   r:   Zcheck_responserd   r   )r@   Zdriver_commandparamsrX   r#   r#   r$   rU   -  s    


zWebDriver.executec             C   s   |  tjd|i dS )zB
        Loads a web page in the current browser session.
        urlN)rU   r   GET)r@   rf   r#   r#   r$   r   I  s    zWebDriver.getc             C   s$   |  tj}|d dk	r |d S dS )zaReturns the title of the current page.

        :Usage:
            title = driver.title
        rR   N )rU   r   Z	GET_TITLE)r@   respr#   r#   r$   titleO  s    zWebDriver.titlec             C   s   | j tj|dS )aF  Finds an element by id.

        :Args:
         - id\_ - The id of the element to be found.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_id('foo')
        )byrR   )find_elementr   ID)r@   id_r#   r#   r$   find_element_by_idY  s    zWebDriver.find_element_by_idc             C   s   | j tj|dS )a;  
        Finds multiple elements by id.

        :Args:
         - id\_ - The id of the elements to be found.

        :Returns:
         - list of WebElement - a list with elements if any was found.  An
           empty list if not

        :Usage:
            elements = driver.find_elements_by_id('foo')
        )rk   rR   )find_elementsr   rm   )r@   rn   r#   r#   r$   find_elements_by_idj  s    zWebDriver.find_elements_by_idc             C   s   | j tj|dS )ae  
        Finds an element by xpath.

        :Args:
         - xpath - The xpath locator of the element to find.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_xpath('//div/td[1]')
        )rk   rR   )rl   r   XPATH)r@   xpathr#   r#   r$   find_element_by_xpathz  s    zWebDriver.find_element_by_xpathc             C   s   | j tj|dS )ah  
        Finds multiple elements by xpath.

        :Args:
         - xpath - The xpath locator of the elements to be found.

        :Returns:
         - list of WebElement - a list with elements if any was found.  An
           empty list if not

        :Usage:
            elements = driver.find_elements_by_xpath("//div[contains(@class, 'foo')]")
        )rk   rR   )rp   r   rr   )r@   rs   r#   r#   r$   find_elements_by_xpath  s    z WebDriver.find_elements_by_xpathc             C   s   | j tj|dS )ag  
        Finds an element by link text.

        :Args:
         - link_text: The text of the element to be found.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_link_text('Sign In')
        )rk   rR   )rl   r   	LINK_TEXT)r@   	link_textr#   r#   r$   find_element_by_link_text  s    z#WebDriver.find_element_by_link_textc             C   s   | j tj|dS )aJ  
        Finds elements by link text.

        :Args:
         - link_text: The text of the elements to be found.

        :Returns:
         - list of webelement - a list with elements if any was found.  an
           empty list if not

        :Usage:
            elements = driver.find_elements_by_link_text('Sign In')
        )rk   rR   )rp   r   rv   )r@   textr#   r#   r$   find_elements_by_link_text  s    z$WebDriver.find_elements_by_link_textc             C   s   | j tj|dS )a  
        Finds an element by a partial match of its link text.

        :Args:
         - link_text: The text of the element to partially match on.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_partial_link_text('Sign')
        )rk   rR   )rl   r   PARTIAL_LINK_TEXT)r@   rw   r#   r#   r$   !find_element_by_partial_link_text  s    z+WebDriver.find_element_by_partial_link_textc             C   s   | j tj|dS )ao  
        Finds elements by a partial match of their link text.

        :Args:
         - link_text: The text of the element to partial match on.

        :Returns:
         - list of webelement - a list with elements if any was found.  an
           empty list if not

        :Usage:
            elements = driver.find_elements_by_partial_link_text('Sign')
        )rk   rR   )rp   r   r{   )r@   rw   r#   r#   r$   "find_elements_by_partial_link_text  s    z,WebDriver.find_elements_by_partial_link_textc             C   s   | j tj|dS )aP  
        Finds an element by name.

        :Args:
         - name: The name of the element to find.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_name('foo')
        )rk   rR   )rl   r   NAME)r@   rO   r#   r#   r$   find_element_by_name  s    zWebDriver.find_element_by_namec             C   s   | j tj|dS )a3  
        Finds elements by name.

        :Args:
         - name: The name of the elements to find.

        :Returns:
         - list of webelement - a list with elements if any was found.  an
           empty list if not

        :Usage:
            elements = driver.find_elements_by_name('foo')
        )rk   rR   )rp   r   r~   )r@   rO   r#   r#   r$   find_elements_by_name  s    zWebDriver.find_elements_by_namec             C   s   | j tj|dS )aZ  
        Finds an element by tag name.

        :Args:
         - name - name of html tag (eg: h1, a, span)

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_tag_name('h1')
        )rk   rR   )rl   r   TAG_NAME)r@   rO   r#   r#   r$   find_element_by_tag_name  s    z"WebDriver.find_element_by_tag_namec             C   s   | j tj|dS )a<  
        Finds elements by tag name.

        :Args:
         - name - name of html tag (eg: h1, a, span)

        :Returns:
         - list of WebElement - a list with elements if any was found.  An
           empty list if not

        :Usage:
            elements = driver.find_elements_by_tag_name('h1')
        )rk   rR   )rp   r   r   )r@   rO   r#   r#   r$   find_elements_by_tag_name  s    z#WebDriver.find_elements_by_tag_namec             C   s   | j tj|dS )ab  
        Finds an element by class name.

        :Args:
         - name: The class name of the element to find.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_class_name('foo')
        )rk   rR   )rl   r   
CLASS_NAME)r@   rO   r#   r#   r$   find_element_by_class_name$  s    z$WebDriver.find_element_by_class_namec             C   s   | j tj|dS )aE  
        Finds elements by class name.

        :Args:
         - name: The class name of the elements to find.

        :Returns:
         - list of WebElement - a list with elements if any was found.  An
           empty list if not

        :Usage:
            elements = driver.find_elements_by_class_name('foo')
        )rk   rR   )rp   r   r   )r@   rO   r#   r#   r$   find_elements_by_class_name6  s    z%WebDriver.find_elements_by_class_namec             C   s   | j tj|dS )ao  
        Finds an element by css selector.

        :Args:
         - css_selector - CSS selector string, ex: 'a.nav#home'

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            element = driver.find_element_by_css_selector('#foo')
        )rk   rR   )rl   r   CSS_SELECTOR)r@   css_selectorr#   r#   r$   find_element_by_css_selectorF  s    z&WebDriver.find_element_by_css_selectorc             C   s   | j tj|dS )aQ  
        Finds elements by css selector.

        :Args:
         - css_selector - CSS selector string, ex: 'a.nav#home'

        :Returns:
         - list of WebElement - a list with elements if any was found.  An
           empty list if not

        :Usage:
            elements = driver.find_elements_by_css_selector('.foo')
        )rk   rR   )rp   r   r   )r@   r   r#   r#   r$   find_elements_by_css_selectorX  s    z'WebDriver.find_elements_by_css_selectorc             G   s6   t |}d}| jrtj}ntj}| |||dd S )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;')
        N)scriptrI   rR   )r`   rV   r   ZW3C_EXECUTE_SCRIPTZEXECUTE_SCRIPTrU   )r@   r   rI   converted_argscommandr#   r#   r$   execute_scripth  s    zWebDriver.execute_scriptc             G   s2   t |}| jrtj}nt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   rI   rR   )r`   rV   r   ZW3C_EXECUTE_SCRIPT_ASYNCZEXECUTE_ASYNC_SCRIPTrU   )r@   r   rI   r   r   r#   r#   r$   execute_async_script~  s    zWebDriver.execute_async_scriptc             C   s   |  tjd S )zc
        Gets the URL of the current page.

        :Usage:
            driver.current_url
        rR   )rU   r   ZGET_CURRENT_URL)r@   r#   r#   r$   current_url  s    zWebDriver.current_urlc             C   s   |  tjd S )zf
        Gets the source of the current page.

        :Usage:
            driver.page_source
        rR   )rU   r   ZGET_PAGE_SOURCE)r@   r#   r#   r$   page_source  s    zWebDriver.page_sourcec             C   s   |  tj dS )zX
        Closes the current window.

        :Usage:
            driver.close()
        N)rU   r   ZCLOSE)r@   r#   r#   r$   close  s    zWebDriver.closec             C   s    z|  tj W d|   X dS )zq
        Quits the driver and closes every associated window.

        :Usage:
            driver.quit()
        N)rU   r   ZQUITrP   )r@   r#   r#   r$   rH     s    zWebDriver.quitc             C   s*   | j r| tjd S | tjd S dS )zu
        Returns the handle of the current window.

        :Usage:
            driver.current_window_handle
        rR   N)rV   rU   r   ZW3C_GET_CURRENT_WINDOW_HANDLEZGET_CURRENT_WINDOW_HANDLE)r@   r#   r#   r$   current_window_handle  s    zWebDriver.current_window_handlec             C   s*   | j r| tjd S | tjd S dS )z
        Returns the handles of all windows within the current session.

        :Usage:
            driver.window_handles
        rR   N)rV   rU   r   ZW3C_GET_WINDOW_HANDLESZGET_WINDOW_HANDLES)r@   r#   r#   r$   window_handles  s    zWebDriver.window_handlesc             C   s.   d}t j}| jst j}ddi}| || dS )zF
        Maximizes the current window that webdriver is using
        NwindowHandlecurrent)r   ZW3C_MAXIMIZE_WINDOWrV   ZMAXIMIZE_WINDOWrU   )r@   re   r   r#   r#   r$   maximize_window  s    zWebDriver.maximize_windowc             C   s   |  tj dS )zM
        Invokes the window manager-specific 'full screen' operation
        N)rU   r   ZFULLSCREEN_WINDOW)r@   r#   r#   r$   fullscreen_window  s    zWebDriver.fullscreen_windowc             C   s   |  tj dS )zJ
        Invokes the window manager-specific 'minimize' operation
        N)rU   r   ZMINIMIZE_WINDOW)r@   r#   r#   r$   minimize_window  s    zWebDriver.minimize_windowc             C   s   | 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')
        )r=   )r@   r#   r#   r$   	switch_to  s    zWebDriver.switch_toc             C   s   t jdtdd | jjS )z8 Deprecated use driver.switch_to.active_element
        z+use driver.switch_to.active_element insteadr(   )r)   )r/   r0   r1   r=   Zactive_element)r@   r#   r#   r$   switch_to_active_element  s    
z"WebDriver.switch_to_active_elementc             C   s    t jdtdd | j| dS )z0 Deprecated use driver.switch_to.window
        z#use driver.switch_to.window insteadr(   )r)   N)r/   r0   r1   r=   Zwindow)r@   Zwindow_namer#   r#   r$   switch_to_window	  s    
zWebDriver.switch_to_windowc             C   s    t jdtdd | j| dS )z/ Deprecated use driver.switch_to.frame
        z"use driver.switch_to.frame insteadr(   )r)   N)r/   r0   r1   r=   frame)r@   Zframe_referencer#   r#   r$   switch_to_frame  s    
zWebDriver.switch_to_framec             C   s   t jdtdd | j  dS )z9 Deprecated use driver.switch_to.default_content
        z,use driver.switch_to.default_content insteadr(   )r)   N)r/   r0   r1   r=   Zdefault_content)r@   r#   r#   r$   switch_to_default_content  s    
z#WebDriver.switch_to_default_contentc             C   s   t jdtdd | jjS )z/ Deprecated use driver.switch_to.alert
        z"use driver.switch_to.alert insteadr(   )r)   )r/   r0   r1   r=   Zalert)r@   r#   r#   r$   switch_to_alert  s    
zWebDriver.switch_to_alertc             C   s   |  tj dS )zk
        Goes one step backward in the browser history.

        :Usage:
            driver.back()
        N)rU   r   ZGO_BACK)r@   r#   r#   r$   back&  s    zWebDriver.backc             C   s   |  tj dS )zm
        Goes one step forward in the browser history.

        :Usage:
            driver.forward()
        N)rU   r   Z
GO_FORWARD)r@   r#   r#   r$   forward/  s    zWebDriver.forwardc             C   s   |  tj dS )z[
        Refreshes the current page.

        :Usage:
            driver.refresh()
        N)rU   r   ZREFRESH)r@   r#   r#   r$   refresh8  s    zWebDriver.refreshc             C   s   |  tjd S )z
        Returns a set of dictionaries, corresponding to cookies visible in the current session.

        :Usage:
            driver.get_cookies()
        rR   )rU   r   ZGET_ALL_COOKIES)r@   r#   r#   r$   get_cookiesB  s    zWebDriver.get_cookiesc             C   sb   | j r4y| tjd|id S  tk
r0   dS X n*|  }x|D ]}|d |krB|S qBW dS dS )z
        Get a single cookie by name. Returns the cookie if found, None if not.

        :Usage:
            driver.get_cookie('my_cookie')
        rO   rR   N)rV   rU   r   Z
GET_COOKIEr   r   )r@   rO   cookiescookier#   r#   r$   
get_cookieK  s    
zWebDriver.get_cookiec             C   s   |  tjd|i dS )z}
        Deletes a single cookie with the given name.

        :Usage:
            driver.delete_cookie('my_cookie')
        rO   N)rU   r   ZDELETE_COOKIE)r@   rO   r#   r#   r$   delete_cookie^  s    zWebDriver.delete_cookiec             C   s   |  tj dS )zz
        Delete all cookies in the scope of the session.

        :Usage:
            driver.delete_all_cookies()
        N)rU   r   ZDELETE_ALL_COOKIES)r@   r#   r#   r$   delete_all_cookiesg  s    zWebDriver.delete_all_cookiesc             C   s   |  tjd|i dS )a  
        Adds a cookie to your current session.

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

        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})

        r   N)rU   r   Z
ADD_COOKIE)r@   cookie_dictr#   r#   r$   
add_cookiep  s    zWebDriver.add_cookiec             C   sD   | j r&| tjdtt|d i n| tjdt|d i dS )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)
        Zimpliciti  msN)rV   rU   r   SET_TIMEOUTSintfloatZIMPLICIT_WAIT)r@   time_to_waitr#   r#   r$   implicitly_wait  s
    zWebDriver.implicitly_waitc             C   sD   | j r&| tjdtt|d i n| tjdt|d i dS )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   i  r   N)rV   rU   r   r   r   r   ZSET_SCRIPT_TIMEOUT)r@   r   r#   r#   r$   set_script_timeout  s
    zWebDriver.set_script_timeoutc          	   C   sX   y"|  tjdtt|d i W n0 tk
rR   |  tjt|d dd Y nX dS )z
        Set 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)
        ZpageLoadi  z	page load)r   r4   N)rU   r   r   r   r   r   )r@   r   r#   r#   r$   set_page_load_timeout  s    
zWebDriver.set_page_load_timeoutc             C   s|   | j rd|tjkr tj}d| }nD|tjkr2tj}n2|tjkrLtj}d| }n|tjkrdtj}d| }| tj	||dd S )z
        Find an element given a By strategy and locator. Prefer the find_element_by_* methods when
        possible.

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

        :rtype: WebElement
        z	[id="%s"]z.%sz[name="%s"])usingrR   rR   )
rV   r   rm   r   r   r   r~   rU   r   ZFIND_ELEMENT)r@   rk   rR   r#   r#   r$   rl     s    






zWebDriver.find_elementc             C   s   | j rd|tjkr tj}d| }nD|tjkr2tj}n2|tjkrLtj}d| }n|tjkrdtj}d| }| tj	||dd p~g S )z
        Find elements given a By strategy and locator. Prefer the find_elements_by_* methods when
        possible.

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

        :rtype: list of WebElement
        z	[id="%s"]z.%sz[name="%s"])r   rR   rR   )
rV   r   rm   r   r   r   r~   rU   r   ZFIND_ELEMENTS)r@   rk   rR   r#   r#   r$   rp     s    






zWebDriver.find_elementsc             C   s   | j S )zM
        returns the drivers current desired capabilities being used
        )r9   )r@   r#   r#   r$   rA     s    zWebDriver.desired_capabilitiesc          	   C   sj   |  dstdt |  }z>y$t|d}|| W dQ R X W n tk
r\   dS X W d~X d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.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wbNFT)	r   endswithr/   r0   UserWarningget_screenshot_as_pngopenwriteIOError)r@   filenameZpngfr#   r#   r$   get_screenshot_as_file  s    
z WebDriver.get_screenshot_as_filec             C   s
   |  |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')
        )r   )r@   r   r#   r#   r$   save_screenshot  s    zWebDriver.save_screenshotc             C   s   t |  dS )z
        Gets the screenshot of the current window as a binary data.

        :Usage:
            driver.get_screenshot_as_png()
        ascii)base64	b64decodeget_screenshot_as_base64encode)r@   r#   r#   r$   r   !  s    zWebDriver.get_screenshot_as_pngc             C   s   |  tjd S )z
        Gets 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()
        rR   )rU   r   Z
SCREENSHOT)r@   r#   r#   r$   r   *  s    z"WebDriver.get_screenshot_as_base64r   c             C   sR   | j r0|dkrtd | jt|t|d n| tjt|t||d dS )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)
        r   z@Only 'current' window is supported for W3C compatibile browsers.)widthheight)r   r   r   N)rV   r/   r0   set_window_rectr   rU   r   ZSET_WINDOW_SIZE)r@   r   r   r   r#   r#   r$   set_window_size4  s    
zWebDriver.set_window_sizec                sb   t j}| jr(|dkrtd |   n| |d|i  dddk	rP d   fdddD S )	zx
        Gets the width and height of the current window.

        :Usage:
            driver.get_window_size()
        r   z@Only 'current' window is supported for W3C compatibile browsers.r   rR   Nc                s   i | ]} | |qS r#   r#   )r[   r!   )sizer#   r$   
<dictcomp>[  s    z-WebDriver.get_window_size.<locals>.<dictcomp>)r   r   )r   ZGET_WINDOW_SIZErV   r/   r0   get_window_rectrU   r   )r@   r   r   r#   )r   r$   get_window_sizeI  s    

zWebDriver.get_window_sizec             C   sP   | j r.|dkrtd | jt|t|dS | tjt|t||d d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)
        r   z@Only 'current' window is supported for W3C compatibile browsers.)xy)r   r   r   N)rV   r/   r0   r   r   rU   r   ZSET_WINDOW_POSITION)r@   r   r   r   r#   r#   r$   set_window_position]  s    
zWebDriver.set_window_positionc                sJ   | j r"|dkrtd |   n| tjd|id   fdddD S )zx
        Gets the x,y position of the current window.

        :Usage:
            driver.get_window_position()
        r   z@Only 'current' window is supported for W3C compatibile browsers.r   rR   c                s   i | ]} | |qS r#   r#   )r[   r!   )positionr#   r$   r     s    z1WebDriver.get_window_position.<locals>.<dictcomp>)r   r   )rV   r/   r0   r   rU   r   ZGET_WINDOW_POSITION)r@   r   r#   )r   r$   get_window_positiont  s    

zWebDriver.get_window_positionc             C   s   |  tjd S )z
        Gets the x, y coordinates of the window as well as height and width of
        the current window.

        :Usage:
            driver.get_window_rect()
        rR   )rU   r   ZGET_WINDOW_RECT)r@   r#   r#   r$   r     s    zWebDriver.get_window_rectc             C   sD   |dkr(|dkr(|dkr(|dkr(t d| tj||||dd S )a4  
        Sets the x, y coordinates of the window as well as height and width of
        the current window.

        :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)
        Nz'x and y or height and width need values)r   r   r   r   rR   )r   rU   r   ZSET_WINDOW_RECT)r@   r   r   r   r   r#   r#   r$   r     s
    
 zWebDriver.set_window_rectc             C   s   | j S )N)_file_detector)r@   r#   r#   r$   r?     s    zWebDriver.file_detectorc             C   s,   |dkrt dt|ts"t d|| _dS )a9  
        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.
        Nz,You may not set a file detector that is nullz+Detector has to be instance of FileDetector)r   r,   r
   r   )r@   detectorr#   r#   r$   r?     s
    
c             C   s   |  tjd S )zz
        Gets the current orientation of the device

        :Usage:
            orientation = driver.orientation
        rR   )rU   r   ZGET_SCREEN_ORIENTATION)r@   r#   r#   r$   orientation  s    zWebDriver.orientationc             C   s4   ddg}|  |kr(| tjd|i ntddS )z
        Sets the current orientation of the device

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

        :Usage:
            driver.orientation = 'landscape'
        Z	LANDSCAPEZPORTRAITr   z>You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'N)upperrU   r   ZSET_SCREEN_ORIENTATIONr   )r@   rR   Zallowed_valuesr#   r#   r$   r     s    c             C   s   t | S )zI Returns a ApplicationCache Object to interact with the browser app cache)r   )r@   r#   r#   r$   application_cache  s    zWebDriver.application_cachec             C   s   |  tjd S )zf
        Gets a list of the available log types

        :Usage:
            driver.log_types
        rR   )rU   r   ZGET_AVAILABLE_LOG_TYPES)r@   r#   r#   r$   	log_types  s    zWebDriver.log_typesc             C   s   |  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')
        r4   rR   )rU   r   ZGET_LOG)r@   Zlog_typer#   r#   r$   get_log  s    zWebDriver.get_log)r'   NNNFNN)N)N)r   )r   )r   )r   )NNNN)Y__name__
__module____qualname____doc__r   r^   rD   rF   rG   rJ   r   rL   propertyrM   rO   r;   rP   r<   rZ   rc   rd   rU   r   rj   ro   rq   rt   ru   rx   rz   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rm   rl   rp   rA   r   r   r   r   r   r   r   r   r   r   r?   setterr   r   r   r   r#   r#   r#   r$   r&   `   s     
/
$



			
				








r&   )&r   r   r   r/   
contextlibr   r   r   
webelementr   remote_connectionr   Zerrorhandlerr   r   r   rM   r	   r?   r
   r   Zselenium.common.exceptionsr   r   r   Zselenium.webdriver.common.byr   Z1selenium.webdriver.common.html5.application_cacher   
basestringr6   	NameError	frozensetr    r   r%   objectr&   r#   r#   r#   r$   <module>   sB   !