<commit_before>
#--IMPORT_ALL_FROM_FUTURE--#

'''
@date 2014-11-16
@author <NAME> <<EMAIL>>
'''

import types
import time
from . import exceptions
from . import utilities
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

def set_attribute(self, name, value):
    value = utilities.js_string_encode(value)
    script = "arguments[0].setAttribute('%s', '%s');"  % (name, value)
    self._parent.execute_script(script, self)
    
def wait_element(self, by, value, for_appear=True, timeout=-1):
    """
    Wait until the element appear or disappear.
    
    @param timeout: If timeout equal to < 0, it will loop infinite. Otherwise 
    it will loop for timeout seconds and raise a NoSuchElementException 
    exception if can not found any element! 
    """
    
    elapsed_time = 0
    element = None
    last_exception = None
    while True:        
        try:
            element = self.find_element(by=by, value=value)
            if for_appear:
                break
        except exceptions.NoSuchElementException as e:
            last_exception = e
            if not for_appear:
                break
        
        time.sleep(1)
        
        # Check if timeout
        if timeout < 0:
            # Loop for infinite
            continue
        
        elapsed_time += 1
        if elapsed_time < timeout:
            raise last_exception
        
        break
    
    return element

def force_focus(self):
    self._parent.execute_script("arguments[0].focus();", self);

def force_click(self):
    self._parent.execute_script("arguments[0].click();", self);

def _execute(self, command, params=None):
    if hasattr(self, '_parent_frame_path'):
        self._parent.switch_to_default_content()
        self._parent.switch_to_frame(self._parent_frame_path)
        result = self._old_execute(command, params)
        self._parent.switch_to_default_content()
    else:
        result = self._old_execute(command, params)

    return result

def find_element(self, by=By.ID, value=None, parent_frame_path=[]):
    if isinstance(self, WebDriver):
        driver = self
    else:
        driver = self._parent
        
    last_exception = None
    try:
        founded_element = self._old_find_element(by, value)
        founded_element._parent_frame_path = parent_frame_path
        return founded_element        
    except exceptions.NoSuchElementException as e:
        if not driver.is_find_element_recursively:
            raise e<commit_msg>Fixed force_xxx() will not switch to element's frame, that lead StaleElementReferenceException be throw.<commit_after>
#--IMPORT_ALL_FROM_FUTURE--#

'''
@date 2014-11-16
@author <NAME> <<EMAIL>>
'''

import types
import time
from . import exceptions
from . import utilities
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

def set_attribute(self, name, value):
    value = utilities.js_string_encode(value)
    script = "arguments[0].setAttribute('%s', '%s');"  % (name, value)
    self._parent.execute_script(script, self)
    
def wait_element(self, by, value, for_appear=True, timeout=-1):
    """
    Wait until the element appear or disappear.
    
    @param timeout: If timeout equal to < 0, it will loop infinite. Otherwise 
    it will loop for timeout seconds and raise a NoSuchElementException 
    exception if can not found any element! 
    """
    
    elapsed_time = 0
    element = None
    last_exception = None
    while True:        
        try:
            element = self.find_element(by=by, value=value)
            if for_appear:
                break
        except exceptions.NoSuchElementException as e:
            last_exception = e
            if not for_appear:
                break
        
        time.sleep(1)
        
        # Check if timeout
        if timeout < 0:
            # Loop for infinite
            continue
        
        elapsed_time += 1
        if elapsed_time < timeout:
            raise last_exception
        
        break
    
    return element

def force_focus(self):
    if hasattr(self, '_parent_frame_path'):
        self._parent.switch_to_default_content()
        self._parent.switch_to_frame(self._parent_frame_path)
        self._parent.execute_script("arguments[0].focus();", self);
        self._parent.switch_to_default_content()
    else:
        self._parent.execute_script("arguments[0].focus();", self);

def force_click(self):
    if hasattr(self, '_parent_frame_path'):
        self._parent.switch_to_default_content()
        self._parent.switch_to_frame(self._parent_frame_path)
        self._parent.execute_script("arguments[0].click();", self);
        self._parent.switch_to_default_content()
    else:
        self._parent.execute_script("arguments[0].click();", self);

def _execute(self, command, params=None):
    if hasattr(self, '_parent_frame_path'):
        self._parent.switch_to_default_content()
        self._parent.switch_to_frame(self._parent_frame_path)
        result = self._old_execute(command, params)
        self._parent.switch_to_default_content()
    else:
        result = self._old_execute(command, params)

    return result

def find_element(self, by=By.ID, value=None, parent_frame_path=[]):
    if isinstance(self, WebDriver):
        driver = self
    else:
        driver = self._parent
        
    last_exception = None
    try:
        founded_element = self._old_find_element(by, value)
        founded_element._parent_frame_path = parent_frame_path
        return founded_element        
    except exceptions.NoSuchElementException as e:
        if not driver.is_find_element_recursively:
            raise e
{'fraction_non_alphanumeric': 0.09024005607149115, 'fraction_numerical': 0.006308042754512003, 'mean_word_length': 2.589937106918239, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5827621', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1619, 'n_words': 399}