示例#1
0
 def _construct_subtype(self, element, dic, tag_name):
     selector = element.selector
     dic[tag_name] = dic.get(tag_name, 0)
     dic[tag_name] += 1
     kls = nerodia.element_class_for(tag_name)
     selector.update({'index': dic[tag_name] - 1, 'tag_name': tag_name})
     return kls(self.query_scope, selector)
示例#2
0
    def to_subtype(self):
        """
        Cast this Element instance to a more specific subtype
        :Example:

        browser.element(xpath="//input[@type='submit']").to_subtype()  #=> #<Button>
        """
        tag = self.tag_name
        from .button import Button
        from .check_box import CheckBox
        from .file_field import FileField
        from .html_elements import HTMLElement
        from .radio import Radio
        from .text_field import TextField

        if tag == 'input':
            elem_type = self.attribute_value('type')
            if elem_type in Button.VALID_TYPES:
                klass = Button
            elif elem_type == 'checkbox':
                klass = CheckBox
            elif elem_type == 'radio':
                klass = Radio
            elif elem_type == 'file':
                klass = FileField
            else:
                klass = TextField
        else:
            klass = nerodia.element_class_for(tag) or HTMLElement

        return klass(self.query_scope,
                     selector=dict(self.selector, element=self.wd))
示例#3
0
    def __iter__(self):
        """
        Yields each element in collection

        :rtype: iter

        :Example:

        divs = browser.divs(class='kls')
        for div in divs:
             print(div.text)
        """
        from .elements.html_elements import HTMLElement
        from .elements.input import Input
        dic = {}
        for idx, e in enumerate(self._elements):
            selector = dict(self.selector, index=idx, element=e)
            element = self._element_class(self.query_scope, selector)
            if element.__class__ in [HTMLElement, Input]:
                tag_name = self.selector.get('tag_name', element.tag_name)
                dic[tag_name] = dic.get(tag_name, 0)
                dic[tag_name] += 1
                kls = nerodia.element_class_for(tag_name)
                selector.update({
                    'index': dic[tag_name] - 1,
                    'tag_name': tag_name
                })
                yield kls(self.query_scope, selector)
            else:
                yield element
示例#4
0
    def _xpath_adjacent(self, **kwargs):
        from nerodia.elements.html_elements import HTMLElement, HTMLElementCollection
        from nerodia.elements.input import Input
        import nerodia

        plural = kwargs.pop('plural', None)
        index = kwargs.pop('index', None)
        tag_name = kwargs.get('tag_name')

        if not (plural
                or any(isinstance(val, Pattern) for val in kwargs.values())):
            kwargs['index'] = index or 0

        if not plural:
            el = nerodia.element_class_for(tag_name, HTMLElement)(self, kwargs)
            return el.to_subtype() if isinstance(el, Input) else el
        elif tag_name:
            return nerodia.element_class_for('{}_collection'.format(tag_name),
                                             HTMLElementCollection)(self,
                                                                    kwargs)
        else:
            return HTMLElementCollection(self, kwargs)
示例#5
0
    def _xpath_adjacent(self, **kwargs):
        from .elements.html_elements import HTMLElement, HTMLElementCollection
        import nerodia

        plural = kwargs.pop('plural', None)
        index = kwargs.pop('index', None)
        tag_name = kwargs.get('tag_name')

        if not (plural or any(isinstance(val, Pattern) for val in kwargs.values())):
            kwargs['index'] = index or 0

        if not plural and tag_name:
            klass = nerodia.element_class_for(tag_name)
        elif not plural:
            klass = HTMLElement
        elif tag_name:
            klass = nerodia.element_class_for('{}_collection'.format(tag_name),
                                              HTMLElementCollection)
        else:
            klass = HTMLElementCollection

        return klass(self, kwargs)
示例#6
0
 def _wrap_element(scope, element):
     from .elements.html_elements import HTMLElement
     klass = nerodia.element_class_for(
         element.tag_name.lower()) or HTMLElement
     return klass(scope, {'element': element})