<s> import tempfile <EOL> import time <EOL> import re <EOL> import sys <EOL> from contextlib import contextmanager <EOL> from selenium . common . exceptions import NoSuchElementException <EOL> from selenium . webdriver . common . action_chains import ActionChains <EOL> from splinter . driver import DriverAPI , ElementAPI <EOL> from splinter . element_list import ElementList <EOL> from splinter . utils import warn_deprecated <EOL> from splinter . request_handler . status_code import StatusCode <EOL> if sys . version_info [ <NUM_LIT:0> ] > <NUM_LIT:2> : <EOL> _meth_func = '<STR_LIT>' <EOL> _func_name = '<STR_LIT>' <EOL> else : <EOL> _meth_func = '<STR_LIT>' <EOL> _func_name = '<STR_LIT>' <EOL> class switch_window : <EOL> def __init__ ( self , browser , window_handle ) : <EOL> self . browser = browser <EOL> self . window_handle = window_handle <EOL> def __enter__ ( self ) : <EOL> self . current_window_handle = self . browser . driver . current_window_handle <EOL> self . browser . driver . switch_to_window ( self . window_handle ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> if self . current_window_handle in self . browser . driver . window_handles : <EOL> self . browser . driver . switch_to_window ( self . current_window_handle ) <EOL> class Window ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , browser , name ) : <EOL> self . _browser = browser <EOL> self . name = name <EOL> @ property <EOL> def title ( self ) : <EOL> """<STR_LIT>""" <EOL> with switch_window ( self . _browser , self . name ) : <EOL> return self . _browser . title <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> with switch_window ( self . _browser , self . name ) : <EOL> return self . _browser . url <EOL> @ property <EOL> def index ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _browser . driver . window_handles . index ( self . name ) <EOL> @ property <EOL> def prev ( self ) : <EOL> """<STR_LIT>""" <EOL> prev_index = self . index - <NUM_LIT:1> <EOL> prev_handle = self . _browser . driver . window_handles [ prev_index ] <EOL> return Window ( self . _browser , prev_handle ) <EOL> @ property <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> next_index = ( self . index + <NUM_LIT:1> ) % len ( self . _browser . driver . window_handles ) <EOL> next_handle = self . _browser . driver . window_handles [ next_index ] <EOL> return Window ( self . _browser , next_handle ) <EOL> def is_current ( ) : <EOL> doc = "<STR_LIT>" <EOL> def fget ( self ) : <EOL> return self . _browser . driver . current_window_handle == self . name <EOL> def fset ( self , value ) : <EOL> if value is True : <EOL> self . _browser . driver . switch_to_window ( self . name ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return locals ( ) <EOL> is_current = property ( ** is_current ( ) ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> target = self . prev if ( self . is_current and self . prev != self ) else None <EOL> with switch_window ( self . _browser , self . name ) : <EOL> self . _browser . driver . close ( ) <EOL> if target is not None : <EOL> target . is_current = True <EOL> def close_others ( self ) : <EOL> self . is_current = True <EOL> for window in self . _browser . windows : <EOL> if window != self : <EOL> window . close ( ) <EOL> def __eq__ ( self , other ) : <EOL> return self . _browser == other . _browser and self . name == other . name <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . url ) <EOL> class Windows ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , browser ) : <EOL> self . _browser = browser <EOL> def __len__ ( self ) : <EOL> return len ( self . _browser . driver . window_handles ) <EOL> def __getitem__ ( self , key ) : <EOL> window_handles = self . _browser . driver . window_handles <EOL> try : <EOL> return Window ( self . _browser , window_handles [ key ] ) <EOL> except TypeError : <EOL> if key not in window_handles : <EOL> raise KeyError ( key ) <EOL> return Window ( self . _browser , key ) <EOL> def current ( ) : <EOL> doc = "<STR_LIT>" <EOL> def fget ( self ) : <EOL> current_handle = self . _browser . driver . current_window_handle <EOL> return Window ( self . _browser , current_handle ) if current_handle else None <EOL> def fset ( self , value ) : <EOL> self . _browser . driver . switch_to_window ( value . name ) <EOL> return locals ( ) <EOL> current = property ( ** current ( ) ) <EOL> def __repr__ ( self ) : <EOL> return str ( [ Window ( self . _browser , handle ) for handle in self . _browser . driver . window_handles ] ) <EOL> class BaseWebDriver ( DriverAPI ) : <EOL> def __init__ ( self , wait_time = <NUM_LIT:2> ) : <EOL> self . wait_time = wait_time <EOL> self . status_code = None <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . quit ( ) <EOL> @ property <EOL> def title ( self ) : <EOL> return self . driver . title <EOL> @ property <EOL> def html ( self ) : <EOL> return self . driver . page_source <EOL> @ property <EOL> def url ( self ) : <EOL> return self . driver . current_url <EOL> def visit ( self , url ) : <EOL> self . status_code = None <EOL> self . driver . get ( url ) <EOL> self . status_code = StatusCode ( <NUM_LIT:200> , '<STR_LIT:OK>' ) <EOL> def back ( self ) : <EOL> self . driver . back ( ) <EOL> def forward ( self ) : <EOL> self . driver . forward ( ) <EOL> def reload ( self ) : <EOL> self . driver . refresh ( ) <EOL> def execute_script ( self , script ) : <EOL> self . driver . execute_script ( script ) <EOL> def evaluate_script ( self , script ) : <EOL> return self . driver . execute_script ( "<STR_LIT>" % script ) <EOL> def is_element_visible ( self , finder , selector , wait_time = None ) : <EOL> wait_time = wait_time or self . wait_time <EOL> end_time = time . time ( ) + wait_time <EOL> while time . time ( ) < end_time : <EOL> if finder ( selector ) and finder ( selector ) . visible : <EOL> return True <EOL> return False <EOL> def is_element_not_visible ( self , finder , selector , wait_time = None ) : <EOL> wait_time = wait_time or self . wait_time <EOL> end_time = time . time ( ) + wait_time <EOL> while time . time ( ) < end_time : <EOL> element = finder ( selector ) <EOL> if not element or ( element and not element . visible ) : <EOL> return True <EOL> return False <EOL> def is_element_visible_by_css ( self , css_selector , wait_time = None ) : <EOL> return self . is_element_visible ( self . find_by_css , css_selector , wait_time ) <EOL> def is_element_not_visible_by_css ( self , css_selector , wait_time = None ) : <EOL> return self . is_element_not_visible ( self . find_by_css , css_selector , wait_time ) <EOL> def is_element_visible_by_xpath ( self , xpath , wait_time = None ) : <EOL> return self . is_element_visible ( self . find_by_xpath , xpath , wait_time ) <EOL> def is_element_not_visible_by_xpath ( self , xpath , wait_time = None ) : <EOL> return self . is_element_not_visible ( self . find_by_xpath , xpath , wait_time ) <EOL> def is_element_present ( self , finder , selector , wait_time = None ) : <EOL> wait_time = wait_time or self . wait_time <EOL> end_time = time . time ( ) + wait_time <EOL> while time . time ( ) < end_time : <EOL> if finder ( selector ) : <EOL> return True <EOL> return False <EOL> def is_element_not_present ( self , finder , selector , wait_time = None ) : <EOL> wait_time = wait_time or self . wait_time <EOL> end_time = time . time ( ) + wait_time <EOL> while time . time ( ) < end_time : <EOL> if not finder ( selector ) : <EOL> return True <EOL> return False <EOL> def is_element_present_by_css ( self , css_selector , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_css , css_selector , wait_time ) <EOL> def is_element_not_present_by_css ( self , css_selector , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_css , css_selector , wait_time ) <EOL> def is_element_present_by_xpath ( self , xpath , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_xpath , xpath , wait_time ) <EOL> def is_element_not_present_by_xpath ( self , xpath , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_xpath , xpath , wait_time ) <EOL> def is_element_present_by_tag ( self , tag , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_tag , tag , wait_time ) <EOL> def is_element_not_present_by_tag ( self , tag , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_tag , tag , wait_time ) <EOL> def is_element_present_by_name ( self , name , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_name , name , wait_time ) <EOL> def is_element_not_present_by_name ( self , name , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_name , name , wait_time ) <EOL> def is_element_present_by_value ( self , value , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_value , value , wait_time ) <EOL> def is_element_not_present_by_value ( self , value , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_value , value , wait_time ) <EOL> def is_element_present_by_text ( self , text , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_text , text , wait_time ) <EOL> def is_element_not_present_by_text ( self , text , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_text , text , wait_time ) <EOL> def is_element_present_by_id ( self , id , wait_time = None ) : <EOL> return self . is_element_present ( self . find_by_id , id , wait_time ) <EOL> def is_element_not_present_by_id ( self , id , wait_time = None ) : <EOL> return self . is_element_not_present ( self . find_by_id , id , wait_time ) <EOL> def get_alert ( self ) : <EOL> return AlertElement ( self . driver . switch_to_alert ( ) ) <EOL> def is_text_present ( self , text , wait_time = None ) : <EOL> wait_time = wait_time or self . wait_time <EOL> end_time = time . time ( ) + wait_time <EOL> while time . time ( ) < end_time : <EOL> try : <EOL> self . driver . find_element_by_tag_name ( '<STR_LIT:body>' ) . text . index ( text ) <EOL> return True <EOL> except ValueError : <EOL> pass <EOL> except NoSuchElementException : <EOL> pass <EOL> return False <EOL> def is_text_not_present ( self , text , wait_time = None ) : <EOL> wait_time = wait_time or self . wait_time <EOL> end_time = time . time ( ) + wait_time <EOL> while time . time ( ) < end_time : <EOL> try : <EOL> self . driver . find_element_by_tag_name ( '<STR_LIT:body>' ) . text . index ( text ) <EOL> except ValueError : <EOL> return True <EOL> except NoSuchElementException : <EOL> pass <EOL> return False <EOL> @ contextmanager <EOL> def get_iframe ( self , id ) : <EOL> self . driver . switch_to . frame ( id ) <EOL> try : <EOL> yield self <EOL> finally : <EOL> self . driver . switch_to . frame ( None ) <EOL> def find_option_by_value ( self , value ) : <EOL> return self . find_by_xpath ( <EOL> '<STR_LIT>' % value , original_find = "<STR_LIT>" , original_query = value ) <EOL> def find_option_by_text ( self , text ) : <EOL> return self . find_by_xpath ( <EOL> '<STR_LIT>' % text , original_find = "<STR_LIT>" , <EOL> original_query = text ) <EOL> def find_link_by_href ( self , href ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % href , original_find = "<STR_LIT>" , original_query = href ) <EOL> def find_link_by_partial_href ( self , partial_href ) : <EOL> return self . find_by_xpath ( <EOL> '<STR_LIT>' % partial_href , original_find = "<STR_LIT>" , <EOL> original_query = partial_href ) <EOL> def find_link_by_partial_text ( self , partial_text ) : <EOL> return self . find_by_xpath ( <EOL> '<STR_LIT>' % partial_text , <EOL> original_find = "<STR_LIT>" , original_query = partial_text ) <EOL> def find_link_by_text ( self , text ) : <EOL> return self . find_by_xpath ( <EOL> '<STR_LIT>' % text , original_find = "<STR_LIT>" , original_query = text ) <EOL> def find_by ( self , finder , selector , original_find = None , original_query = None ) : <EOL> elements = None <EOL> end_time = time . time ( ) + self . wait_time <EOL> func_name = getattr ( getattr ( finder , _meth_func ) , _func_name ) <EOL> find_by = original_find or func_name [ func_name . rfind ( '<STR_LIT>' ) + <NUM_LIT:4> : ] <EOL> query = original_query or selector <EOL> while time . time ( ) < end_time : <EOL> try : <EOL> elements = finder ( selector ) <EOL> if not isinstance ( elements , list ) : <EOL> elements = [ elements ] <EOL> except NoSuchElementException : <EOL> pass <EOL> if elements : <EOL> return ElementList ( <EOL> [ self . element_class ( element , self ) for element in elements ] , <EOL> find_by = find_by , query = query ) <EOL> return ElementList ( [ ] , find_by = find_by , query = query ) <EOL> def find_by_css ( self , css_selector ) : <EOL> return self . find_by ( <EOL> self . driver . find_elements_by_css_selector , css_selector , original_find = '<STR_LIT>' , <EOL> original_query = css_selector ) <EOL> def find_by_xpath ( self , xpath , original_find = None , original_query = None ) : <EOL> original_find = original_find or "<STR_LIT>" <EOL> original_query = original_query or xpath <EOL> return self . find_by ( <EOL> self . driver . find_elements_by_xpath , xpath , original_find = original_find , <EOL> original_query = original_query ) <EOL> def find_by_name ( self , name ) : <EOL> return self . find_by ( self . driver . find_elements_by_name , name ) <EOL> def find_by_tag ( self , tag ) : <EOL> return self . find_by ( self . driver . find_elements_by_tag_name , tag ) <EOL> def find_by_value ( self , value ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % value , original_find = '<STR_LIT:value>' , original_query = value ) <EOL> def find_by_text ( self , text ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % text , <EOL> original_find = '<STR_LIT:text>' , original_query = text ) <EOL> def find_by_id ( self , id ) : <EOL> return self . find_by ( self . driver . find_element_by_id , id ) <EOL> def fill ( self , name , value ) : <EOL> field = self . find_by_name ( name ) . first <EOL> field . value = value <EOL> attach_file = fill <EOL> def fill_form ( self , field_values ) : <EOL> for name , value in field_values . items ( ) : <EOL> elements = self . find_by_name ( name ) <EOL> element = elements . first <EOL> if element [ '<STR_LIT:type>' ] in [ '<STR_LIT:text>' , '<STR_LIT:password>' , '<STR_LIT>' ] or element . tag_name == '<STR_LIT>' : <EOL> element . value = value <EOL> elif element [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if value : <EOL> element . check ( ) <EOL> else : <EOL> element . uncheck ( ) <EOL> elif element [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> for field in elements : <EOL> if field . value == value : <EOL> field . click ( ) <EOL> elif element . _element . tag_name == '<STR_LIT>' : <EOL> element . find_by_value ( value ) . first . _element . click ( ) <EOL> else : <EOL> element . value = value <EOL> def type ( self , name , value , slowly = False ) : <EOL> element = self . find_by_name ( name ) . first . _element <EOL> if slowly : <EOL> return TypeIterator ( element , value ) <EOL> element . send_keys ( value ) <EOL> return value <EOL> def choose ( self , name , value ) : <EOL> fields = self . find_by_name ( name ) <EOL> for field in fields : <EOL> if field . value == value : <EOL> field . click ( ) <EOL> def check ( self , name ) : <EOL> self . find_by_name ( name ) . first . check ( ) <EOL> def uncheck ( self , name ) : <EOL> self . find_by_name ( name ) . first . uncheck ( ) <EOL> def screenshot ( self , name = None , suffix = '<STR_LIT>' ) : <EOL> name = name or '<STR_LIT>' <EOL> ( fd , filename ) = tempfile . mkstemp ( prefix = name , suffix = suffix ) <EOL> self . driver . get_screenshot_as_file ( filename ) <EOL> return filename <EOL> def select ( self , name , value ) : <EOL> self . find_by_xpath ( '<STR_LIT>' % ( name , value ) ) . first . _element . click ( ) <EOL> def select_by_text ( self , name , text ) : <EOL> self . find_by_xpath ( '<STR_LIT>' % ( name , text ) ) . first . _element . click ( ) <EOL> def quit ( self ) : <EOL> self . driver . quit ( ) <EOL> @ property <EOL> def cookies ( self ) : <EOL> return self . _cookie_manager <EOL> @ property <EOL> def windows ( self ) : <EOL> return Windows ( self ) <EOL> class TypeIterator ( object ) : <EOL> def __init__ ( self , element , keys ) : <EOL> self . _element = element <EOL> self . _keys = keys <EOL> def __iter__ ( self ) : <EOL> for key in self . _keys : <EOL> self . _element . send_keys ( key ) <EOL> yield key <EOL> class WebDriverElement ( ElementAPI ) : <EOL> def __init__ ( self , element , parent ) : <EOL> self . _element = element <EOL> self . parent = parent <EOL> self . action_chains = ActionChains ( parent . driver ) <EOL> def _get_value ( self ) : <EOL> return self [ '<STR_LIT:value>' ] or self . _element . text <EOL> def _set_value ( self , value ) : <EOL> if self . _element . get_attribute ( '<STR_LIT:type>' ) != '<STR_LIT:file>' : <EOL> self . _element . clear ( ) <EOL> self . _element . send_keys ( value ) <EOL> value = property ( _get_value , _set_value ) <EOL> @ property <EOL> def text ( self ) : <EOL> return self . _element . text <EOL> @ property <EOL> def tag_name ( self ) : <EOL> return self . _element . tag_name <EOL> def fill ( self , value ) : <EOL> self . value = value <EOL> def select ( self , value ) : <EOL> self . find_by_xpath ( <EOL> '<STR_LIT>' % ( self [ "<STR_LIT:name>" ] , value ) ) . _element . click ( ) <EOL> def select_by_text ( self , text ) : <EOL> self . find_by_xpath ( '<STR_LIT>' % ( self [ "<STR_LIT:name>" ] , text ) ) . _element . click ( ) <EOL> def type ( self , value , slowly = False ) : <EOL> if slowly : <EOL> return TypeIterator ( self . _element , value ) <EOL> self . _element . send_keys ( value ) <EOL> return value <EOL> def click ( self ) : <EOL> self . _element . click ( ) <EOL> def check ( self ) : <EOL> if not self . checked : <EOL> self . _element . click ( ) <EOL> def uncheck ( self ) : <EOL> if self . checked : <EOL> self . _element . click ( ) <EOL> @ property <EOL> def checked ( self ) : <EOL> return self . _element . is_selected ( ) <EOL> selected = checked <EOL> @ property <EOL> def visible ( self ) : <EOL> return self . _element . is_displayed ( ) <EOL> @ property <EOL> def html ( self ) : <EOL> return self [ '<STR_LIT>' ] <EOL> @ property <EOL> def outer_html ( self ) : <EOL> return self [ '<STR_LIT>' ] <EOL> def find_by_css ( self , selector , original_find = None , original_query = None ) : <EOL> find_by = original_find or '<STR_LIT>' <EOL> query = original_query or selector <EOL> elements = self . _element . find_elements_by_css_selector ( selector ) <EOL> return ElementList ( <EOL> [ self . __class__ ( element , self . parent ) for element in elements ] , find_by = find_by , query = query ) <EOL> def find_by_xpath ( self , selector , original_find = None , original_query = None ) : <EOL> elements = ElementList ( self . _element . find_elements_by_xpath ( selector ) ) <EOL> return ElementList ( <EOL> [ self . __class__ ( element , self . parent ) for element in elements ] , find_by = '<STR_LIT>' , query = selector ) <EOL> def find_by_name ( self , name ) : <EOL> elements = ElementList ( self . _element . find_elements_by_name ( name ) ) <EOL> return ElementList ( <EOL> [ self . __class__ ( element , self . parent ) for element in elements ] , find_by = '<STR_LIT:name>' , query = name ) <EOL> def find_by_tag ( self , tag ) : <EOL> elements = ElementList ( self . _element . find_elements_by_tag_name ( tag ) ) <EOL> return ElementList ( <EOL> [ self . __class__ ( element , self . parent ) for element in elements ] , find_by = '<STR_LIT>' , query = tag ) <EOL> def find_by_value ( self , value ) : <EOL> selector = '<STR_LIT>' % value <EOL> return self . find_by_css ( selector , original_find = '<STR_LIT:value>' , original_query = value ) <EOL> def find_by_text ( self , text ) : <EOL> selector = '<STR_LIT>' % text <EOL> return self . find_by_xpath ( selector , original_find = '<STR_LIT:text>' , original_query = text ) <EOL> def find_by_id ( self , id ) : <EOL> elements = ElementList ( self . _element . find_elements_by_id ( id ) ) <EOL> return ElementList ( <EOL> [ self . __class__ ( element , self . parent ) for element in elements ] , find_by = '<STR_LIT:id>' , query = id ) <EOL> def has_class ( self , class_name ) : <EOL> return bool ( re . search ( r'<STR_LIT>' + re . escape ( class_name ) + r'<STR_LIT>' , self [ '<STR_LIT:class>' ] ) ) <EOL> def mouse_over ( self ) : <EOL> """<STR_LIT>""" <EOL> self . action_chains . move_to_element ( self . _element ) <EOL> self . action_chains . perform ( ) <EOL> def mouse_out ( self ) : <EOL> """<STR_LIT>""" <EOL> self . action_chains . move_by_offset ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . action_chains . perform ( ) <EOL> mouseover = warn_deprecated ( mouse_over , '<STR_LIT>' ) <EOL> def double_click ( self ) : <EOL> """<STR_LIT>""" <EOL> self . action_chains . double_click ( self . _element ) <EOL> self . action_chains . perform ( ) <EOL> def right_click ( self ) : <EOL> """<STR_LIT>""" <EOL> self . action_chains . context_click ( self . _element ) <EOL> self . action_chains . perform ( ) <EOL> def drag_and_drop ( self , droppable ) : <EOL> """<STR_LIT>""" <EOL> self . action_chains . drag_and_drop ( self . _element , droppable . _element ) <EOL> self . action_chains . perform ( ) <EOL> def __getitem__ ( self , attr ) : <EOL> return self . _element . get_attribute ( attr ) <EOL> class AlertElement ( object ) : <EOL> def __init__ ( self , alert ) : <EOL> self . _alert = alert <EOL> self . text = alert . text <EOL> def accept ( self ) : <EOL> self . _alert . accept ( ) <EOL> def dismiss ( self ) : <EOL> self . _alert . dismiss ( ) <EOL> def fill_with ( self , text ) : <EOL> self . _alert . send_keys ( text ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> pass </s>
