<s> from __future__ import with_statement <EOL> import os . path <EOL> import re <EOL> import time <EOL> import sys <EOL> import lxml . html <EOL> from lxml . cssselect import CSSSelector <EOL> from splinter . driver import DriverAPI , ElementAPI <EOL> from splinter . driver . element_present import ElementPresentMixIn <EOL> from splinter . element_list import ElementList <EOL> from splinter . exceptions import ElementDoesNotExist <EOL> class LxmlDriver ( ElementPresentMixIn , DriverAPI ) : <EOL> def __init__ ( self , user_agent = None , wait_time = <NUM_LIT:2> ) : <EOL> self . wait_time = wait_time <EOL> self . _history = [ ] <EOL> self . _last_urls = [ ] <EOL> self . _forms = { } <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> pass <EOL> def _do_method ( self , action , url , data = None ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % self . driver_name ) <EOL> def visit ( self , url ) : <EOL> self . _do_method ( '<STR_LIT>' , url ) <EOL> def serialize ( self , form ) : <EOL> data = { } <EOL> for k , v in form . fields . items ( ) : <EOL> if v is None : <EOL> continue <EOL> if isinstance ( v , lxml . html . MultipleSelectOptions ) : <EOL> data [ k ] = [ val for val in v ] <EOL> else : <EOL> data [ k ] = v <EOL> for key in form . inputs . keys ( ) : <EOL> input = form . inputs [ key ] <EOL> if getattr ( input , '<STR_LIT:type>' , '<STR_LIT>' ) == '<STR_LIT:file>' and key in data : <EOL> data [ key ] = open ( data [ key ] , '<STR_LIT:rb>' ) <EOL> return data <EOL> def submit ( self , form ) : <EOL> method = form . attrib . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> action = form . attrib . get ( '<STR_LIT:action>' , '<STR_LIT>' ) <EOL> if action . strip ( ) != '<STR_LIT:.>' : <EOL> url = os . path . join ( self . _url , action ) <EOL> else : <EOL> url = self . _url <EOL> self . _url = url <EOL> data = self . serialize ( form ) <EOL> self . _do_method ( method , url , data = data ) <EOL> return self . _response <EOL> def submit_data ( self , form ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % self . driver_name ) <EOL> def back ( self ) : <EOL> self . _last_urls . insert ( <NUM_LIT:0> , self . url ) <EOL> self . visit ( self . _last_urls [ <NUM_LIT:1> ] ) <EOL> def forward ( self ) : <EOL> try : <EOL> self . visit ( self . _last_urls . pop ( ) ) <EOL> except IndexError : <EOL> pass <EOL> def reload ( self ) : <EOL> self . visit ( self . _url ) <EOL> def quit ( self ) : <EOL> pass <EOL> @ property <EOL> def htmltree ( self ) : <EOL> try : <EOL> return self . _html <EOL> except AttributeError : <EOL> self . _html = lxml . html . fromstring ( self . html ) <EOL> return self . _html <EOL> @ property <EOL> def title ( self ) : <EOL> html = self . htmltree <EOL> return html . xpath ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . text_content ( ) . strip ( ) <EOL> @ property <EOL> def html ( self ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % <EOL> self . driver_name ) <EOL> @ property <EOL> def url ( self ) : <EOL> return self . _url <EOL> def find_option_by_value ( self , value ) : <EOL> html = self . htmltree <EOL> element = html . xpath ( '<STR_LIT>' % value ) [ <NUM_LIT:0> ] <EOL> control = LxmlControlElement ( element . getparent ( ) , self ) <EOL> return ElementList ( [ LxmlOptionElement ( element , control ) ] , find_by = "<STR_LIT:value>" , query = value ) <EOL> def find_option_by_text ( self , text ) : <EOL> html = self . htmltree <EOL> element = html . xpath ( '<STR_LIT>' % text ) [ <NUM_LIT:0> ] <EOL> control = LxmlControlElement ( element . getparent ( ) , self ) <EOL> return ElementList ( [ LxmlOptionElement ( element , control ) ] , find_by = "<STR_LIT:text>" , query = text ) <EOL> def find_by_css ( self , selector ) : <EOL> xpath = CSSSelector ( selector ) . path <EOL> return self . find_by_xpath ( xpath , original_find = "<STR_LIT>" , original_selector = selector ) <EOL> def find_by_xpath ( self , xpath , original_find = None , original_selector = None ) : <EOL> html = self . htmltree <EOL> elements = [ ] <EOL> for xpath_element in html . xpath ( xpath ) : <EOL> if self . _element_is_link ( xpath_element ) : <EOL> return self . _find_links_by_xpath ( xpath ) <EOL> elif self . _element_is_control ( xpath_element ) : <EOL> elements . append ( ( LxmlControlElement , xpath_element ) ) <EOL> else : <EOL> elements . append ( ( LxmlElement , xpath_element ) ) <EOL> find_by = original_find or "<STR_LIT>" <EOL> query = original_selector or xpath <EOL> return ElementList ( <EOL> [ element_class ( element , self ) for element_class , element in elements ] , <EOL> find_by = find_by , query = query ) <EOL> def find_by_tag ( self , tag ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % tag , original_find = "<STR_LIT>" , original_selector = tag ) <EOL> def find_by_value ( self , value ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % value , original_find = "<STR_LIT:value>" , original_selector = value ) <EOL> def find_by_text ( self , text ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % text , <EOL> original_find = "<STR_LIT:text>" , original_selector = text ) <EOL> def find_by_id ( self , id_value ) : <EOL> return self . find_by_xpath ( <EOL> '<STR_LIT>' % id_value , original_find = "<STR_LIT:id>" , original_selector = id_value ) <EOL> def find_by_name ( self , name ) : <EOL> html = self . htmltree <EOL> xpath = '<STR_LIT>' % name <EOL> elements = [ ] <EOL> for xpath_element in html . xpath ( xpath ) : <EOL> elements . append ( xpath_element ) <EOL> find_by = "<STR_LIT:name>" <EOL> query = xpath <EOL> return ElementList ( <EOL> [ LxmlControlElement ( element , self ) for element in elements ] , <EOL> find_by = find_by , query = query ) <EOL> def find_link_by_text ( self , text ) : <EOL> return self . _find_links_by_xpath ( "<STR_LIT>" % text ) <EOL> def find_link_by_href ( self , href ) : <EOL> return self . _find_links_by_xpath ( "<STR_LIT>" % href ) <EOL> def find_link_by_partial_href ( self , partial_href ) : <EOL> return self . _find_links_by_xpath ( "<STR_LIT>" % partial_href ) <EOL> def find_link_by_partial_text ( self , partial_text ) : <EOL> return self . _find_links_by_xpath ( "<STR_LIT>" % partial_text ) <EOL> def fill ( self , name , value ) : <EOL> self . find_by_name ( name = name ) . first . fill ( value ) <EOL> def fill_form ( self , field_values ) : <EOL> for name , value in field_values . items ( ) : <EOL> element = self . find_by_name ( name ) <EOL> control = element . first . _control <EOL> control_type = control . get ( '<STR_LIT:type>' ) <EOL> if control_type == '<STR_LIT>' : <EOL> if value : <EOL> control . value = value <EOL> else : <EOL> control . value = [ ] <EOL> elif control_type == '<STR_LIT>' : <EOL> control . value = value <EOL> elif control_type == '<STR_LIT>' : <EOL> if isinstance ( value , list ) : <EOL> control . value = value <EOL> else : <EOL> control . value = [ value ] <EOL> else : <EOL> control . value = value <EOL> def choose ( self , name , value ) : <EOL> self . find_by_name ( name ) . first . _control . value = value <EOL> def check ( self , name ) : <EOL> control = self . find_by_name ( name ) . first . _control <EOL> control . value = [ '<STR_LIT>' ] <EOL> def uncheck ( self , name ) : <EOL> control = self . find_by_name ( name ) . first . _control <EOL> control . value = [ ] <EOL> def attach_file ( self , name , file_path ) : <EOL> control = self . find_by_name ( name ) . first . _control <EOL> control . value = file_path <EOL> def _find_links_by_xpath ( self , xpath ) : <EOL> html = self . htmltree <EOL> links = html . xpath ( xpath ) <EOL> return ElementList ( <EOL> [ LxmlLinkElement ( link , self ) for link in links ] , find_by = "<STR_LIT>" , query = xpath ) <EOL> def select ( self , name , value ) : <EOL> self . find_by_name ( name ) . first . _control . value = value <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> if self . _is_text_present ( text ) : <EOL> return True <EOL> return False <EOL> def _is_text_present ( self , text ) : <EOL> try : <EOL> body = self . find_by_tag ( '<STR_LIT:body>' ) . first <EOL> return text in body . text <EOL> except ElementDoesNotExist : <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> if not self . _is_text_present ( text ) : <EOL> return True <EOL> return False <EOL> def _element_is_link ( self , element ) : <EOL> return element . tag == '<STR_LIT:a>' <EOL> def _element_is_control ( self , element ) : <EOL> return hasattr ( element , '<STR_LIT:type>' ) <EOL> @ property <EOL> def cookies ( self ) : <EOL> return self . _cookie_manager <EOL> re_extract_inner_html = re . compile ( r'<STR_LIT>' ) <EOL> class LxmlElement ( ElementAPI ) : <EOL> def __init__ ( self , element , parent ) : <EOL> self . _element = element <EOL> self . parent = parent <EOL> def __getitem__ ( self , attr ) : <EOL> return self . _element . attrib [ attr ] <EOL> def find_by_css ( self , selector ) : <EOL> elements = self . _element . cssselect ( selector ) <EOL> return ElementList ( [ self . __class__ ( element , self ) for element in elements ] ) <EOL> def find_by_xpath ( self , selector ) : <EOL> elements = self . _element . xpath ( selector ) <EOL> return ElementList ( [ self . __class__ ( element , self ) for element in elements ] ) <EOL> def find_by_name ( self , name ) : <EOL> elements = self . _element . cssselect ( '<STR_LIT>' % name ) <EOL> return ElementList ( [ self . __class__ ( element , self ) for element in elements ] ) <EOL> def find_by_tag ( self , name ) : <EOL> elements = self . _element . cssselect ( name ) <EOL> return ElementList ( [ self . __class__ ( element , self ) for element in elements ] ) <EOL> def find_by_value ( self , value ) : <EOL> elements = self . _element . cssselect ( '<STR_LIT>' % value ) <EOL> return ElementList ( [ self . __class__ ( element , self ) for element in elements ] ) <EOL> def find_by_text ( self , text ) : <EOL> return self . find_by_xpath ( '<STR_LIT>' % text ) <EOL> def find_by_id ( self , id ) : <EOL> elements = self . _element . cssselect ( '<STR_LIT>' % id ) <EOL> return ElementList ( [ self . __class__ ( element , self ) for element in elements ] ) <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _element . text_content ( ) <EOL> @ property <EOL> def text ( self ) : <EOL> return self . value <EOL> @ property <EOL> def outer_html ( self ) : <EOL> return lxml . html . tostring ( self . _element , encoding = '<STR_LIT>' ) . strip ( ) <EOL> @ property <EOL> def html ( self ) : <EOL> return re_extract_inner_html . match ( self . outer_html ) . group ( <NUM_LIT:1> ) <EOL> def has_class ( self , class_name ) : <EOL> return len ( self . _element . find_class ( class_name ) ) > <NUM_LIT:0> <EOL> class LxmlLinkElement ( LxmlElement ) : <EOL> def __init__ ( self , element , parent ) : <EOL> super ( LxmlLinkElement , self ) . __init__ ( element , parent ) <EOL> self . _browser = parent <EOL> def __getitem__ ( self , attr ) : <EOL> return super ( LxmlLinkElement , self ) . __getitem__ ( attr ) <EOL> def click ( self ) : <EOL> return self . _browser . visit ( self [ "<STR_LIT>" ] ) <EOL> class LxmlControlElement ( LxmlElement ) : <EOL> def __init__ ( self , control , parent ) : <EOL> self . _control = control <EOL> self . parent = parent <EOL> def __getitem__ ( self , attr ) : <EOL> return self . _control . attrib [ attr ] <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _control . value <EOL> @ property <EOL> def checked ( self ) : <EOL> return bool ( self . _control . value ) <EOL> def click ( self ) : <EOL> parent_form = self . _get_parent_form ( ) <EOL> return self . parent . submit_data ( parent_form ) <EOL> def fill ( self , value ) : <EOL> parent_form = self . _get_parent_form ( ) <EOL> if sys . version_info [ <NUM_LIT:0> ] > <NUM_LIT:2> : <EOL> parent_form . fields [ self [ '<STR_LIT:name>' ] ] = value <EOL> else : <EOL> if not isinstance ( value , unicode ) : <EOL> value = value . decode ( '<STR_LIT:utf-8>' ) <EOL> parent_form . fields [ self [ '<STR_LIT:name>' ] ] = value <EOL> def select ( self , value ) : <EOL> self . _control . value = value <EOL> def _get_parent_form ( self ) : <EOL> parent_form = next ( self . _control . iterancestors ( '<STR_LIT>' ) ) <EOL> return self . parent . _forms . setdefault ( parent_form . _name ( ) , parent_form ) <EOL> class LxmlOptionElement ( LxmlElement ) : <EOL> def __init__ ( self , control , parent ) : <EOL> self . _control = control <EOL> self . parent = parent <EOL> def __getitem__ ( self , attr ) : <EOL> return self . _control . attrib [ attr ] <EOL> @ property <EOL> def text ( self ) : <EOL> return self . _control . text <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _control . attrib [ '<STR_LIT:value>' ] <EOL> @ property <EOL> def selected ( self ) : <EOL> return self . parent . value == self . value </s>
