splinter icon indicating copy to clipboard operation
splinter copied to clipboard

Unable to slice on ElementList in python3

Open djmunro opened this issue 7 years ago • 3 comments

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)
        ]
    ...

djmunro avatar Aug 08 '19 16:08 djmunro

This issue was fixed in the 0.11.0 release.

javascript-error avatar Aug 11 '19 04:08 javascript-error

@jsfehler Do you happen to know when 0.11.0 will be released?

djmunro avatar Aug 12 '19 13:08 djmunro

@djmunro June 27th, 2019

javascript-error avatar Aug 12 '19 13:08 javascript-error