Can't slice result of find_by_tag
Here's example straight from my code:
all_raw_msgs = self.browser.find_by_id('messages').first.find_by_tag('li')
new_raw_msgs = all_raw_msgs[self.message_count:-1]
I get error 'slice(0, -1, None) is not JSON serializable', so I looked up source code and found this:
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))
Why case of non-integer index is special? This is the line that makes attempt to slice fail. What is reason for this? Thanks for your time.
this line that makes possible to execute a command in the first element without the first attr. e.g:
elements = browser.find_by_tag('li')
elements.click()
I believe that you found a bug that should be fixed. But I believethat convert the find_by_* return to list is a workaround for this problem:
all_raw_msgs = list(self.browser.find_by_id('messages').first.find_by_tag('li'))
new_raw_msgs = all_raw_msgs[self.message_count:-1]
Can you try it?
I'm having a similar issue, except I'm adding in the .html function. My code looks as follows:
post = browser.find_by_css('.fm-post')
block = post[0].find_by_tag('p')
notes = block[3:]
notes.html # or block[3:].html
I've tried multiple different types of simple declarations to try and print each p tag from index 3 on, including the list solution provided by @andrewsmedina but they all result in the slice error from the OP, just with different numbers. Selecting a single index works fine, as I use that throughout my code, but anything more is a no go.
@andrewsmedina A simple fix to this could be checking if the index is a slice or not:
def __getitem__(self, index):
if not isinstance(index, int) and not isinstance(index, slice):
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
)
)