Unable to slice on ElementList in python3
Issue
Applying a type(slice) to an ElementList causes a TypeError: Object of type slice is not JSON serializable
This happens because of the getitem implantation
py3/lib/python3.7/site-packages/splinter/element_list.py
def __getitem__(self, index):
if not isinstance(index, int):
return self.first[index]
...
Slicing in python 3 will cause index to be type(slice). So when if not isinstance(index, int)
get's evaluated, the code will return return self.first[index].
However this is incorrect. We want to slice(take a subset of the ElementList), but the code being execute will try and slice on the first item of the list.
Instead the code should handle type(slice) independently. Here's an example implementation...
def __getitem__(self, index):
if isinstance(index, slice):
# Handle none vales e.g. slice(None, 3, None) -> slice(0, 3, 1)
return [
self[i]
for i in range(index.start or 0, index.stop or len(self), index.step or 1)
]
...
This issue was fixed in the 0.11.0 release.
@jsfehler Do you happen to know when 0.11.0 will be released?
@djmunro June 27th, 2019