<s> import re <EOL> try : <EOL> from scrapy . selector import Selector , XPathSelectorList <EOL> except ImportError : <EOL> from scrapy . selector import XPathSelector as Selector , XPathSelectorList <EOL> _UNSUPPORTED_XPATH_ENDING = re . compile ( r'<STR_LIT>' ) <EOL> class WebdriverXPathSelector ( Selector ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response = None , webdriver = None , element = None , <EOL> * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = response <EOL> super ( WebdriverXPathSelector , self ) . __init__ ( * args , ** kwargs ) <EOL> self . response = response <EOL> self . webdriver = webdriver or response . webdriver <EOL> self . element = element <EOL> def _make_result ( self , result ) : <EOL> if type ( result ) is not list : <EOL> result = [ result ] <EOL> return [ self . __class__ ( webdriver = self . webdriver , element = e ) <EOL> for e in result ] <EOL> def select ( self , xpath ) : <EOL> """<STR_LIT>""" <EOL> xpathev = self . element if self . element else self . webdriver <EOL> ending = _UNSUPPORTED_XPATH_ENDING . match ( xpath ) <EOL> atsign = parens = None <EOL> if ending : <EOL> match , atsign , name , parens = ending . groups ( ) <EOL> if atsign : <EOL> xpath = xpath [ : - len ( name ) - <NUM_LIT:2> ] <EOL> elif parens and name == '<STR_LIT:text>' : <EOL> xpath = xpath [ : - len ( name ) - <NUM_LIT:3> ] <EOL> result = self . _make_result ( xpathev . find_elements_by_xpath ( xpath ) ) <EOL> if atsign : <EOL> result = ( _NodeAttribute ( r . element , name ) for r in result ) <EOL> elif parens and result and name == '<STR_LIT:text>' : <EOL> result = ( _TextNode ( self . webdriver , r . element ) for r in result ) <EOL> return XPathSelectorList ( result ) <EOL> def select_script ( self , script , * args ) : <EOL> """<STR_LIT>""" <EOL> result = self . webdriver . execute_script ( script , * args ) <EOL> return XPathSelectorList ( self . _make_result ( result ) ) <EOL> def extract ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . element . text if self . element else None <EOL> class _NodeAttribute ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , element , attribute ) : <EOL> self . element = element <EOL> self . attribute = attribute <EOL> def extract ( self ) : <EOL> return self . element . get_attribute ( self . attribute ) <EOL> class _TextNode ( object ) : <EOL> """<STR_LIT>""" <EOL> JS_FIND_FIRST_TEXT_NODE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , webdriver , element ) : <EOL> self . element = element <EOL> self . webdriver = webdriver <EOL> def extract ( self ) : <EOL> args = ( self . JS_FIND_FIRST_TEXT_NODE , self . element ) <EOL> return self . webdriver . execute_script ( * args ) </s>
