Repository navigation
Expand file tree
/
Copy pathelement_list.py
More file actions
75 lines (60 loc) · 2.21 KB
/
Copy pathelement_list.py
File metadata and controls
75 lines (60 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from splinter.exceptions import ElementDoesNotExist
class ElementList(list):
"""
List of elements. Each member of the list is (usually) an instance
of :class:`ElementAPI <splinter.driver.ElementAPI>`.
Beyond the traditional list methods, the ``ElementList`` provides some
other methods, listed below.
There is a peculiar behavior on ElementList: you never get an
``IndexError``. Instead, you can an :class:`ElementDoesNotExist
<splinter.exceptions.ElementDoesNotExist>` exception when trying to
access an inexistent item in the list:
>>> element_list = ElementList([])
>>> element_list[0] # raises ElementDoesNotExist
"""
def __init__(self, list, driver=None, find_by=None, query=None):
"""
Creates the list.
"""
self.extend(list)
self.driver = driver
self.find_by = find_by
self.query = query
def __getitem__(self, index):
if not isinstance(index, int):
return self.first[index]
try:
return super(ElementList, self).__getitem__(index)
except IndexError:
raise ElementDoesNotExist(
u'no elements could be found with {0} "{1}"'.format(
self.find_by, self.query))
@property
def first(self):
"""
An alias to the first element of the list:
>>> assert element_list[0] == element_list.first
"""
return self[0]
@property
def last(self):
"""
An alias to the last element of the list:
>>> assert element_list[-1] == element_list.last
"""
return self[-1]
def is_empty(self):
"""
Returns ``True`` if the list is empty.
"""
return len(self) == 0
def __getattr__(self, name):
try:
return getattr(self.first, name)
except (ElementDoesNotExist, AttributeError):
raise AttributeError(u"'{0}' object has no attribute '{1}'".format(
self.__class__.__name__, name))