cobrateam/splinter — pull request #313 by jvrsantacruz (2014-04-28 10:33:20+00:00)
Title: Fixes UnicodeEncodeError when raising ElementDoesNotExist with unicode q...

Even though performing unicode element queries perfectly works, there is a small annoyance.

When accessing a ElementList when the query returned no results, a `ElementDoesNotExist` exception should be raised, containing a message putting together the method and the given query, but a `UnicodeEncodeError` is raised instead.

This can be easily reproduced using `python 2.7`:

```python
from splinter import Browser
browser = Browser()
browser.find_by_css(u'a[title=título]')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
----> 1 browser.find_by_css(u'a[title="título"]').first

/home/user/.virtualenvs/env/lib/python2.7/site-packages/splinter/element_list.pyc in first(self)
51 >>> assert element_list[0] == element_list.first
52 """
---> 53 return self[0]
54
55 @property

/home/user/.virtualenvs/env/lib/python2.7/site-packages/splinter/element_list.pyc in __getitem__(self, index)
42 raise ElementDoesNotExist(
43 'no elements could be found with {0} "{1}"'.format(
---> 44 self.find_by, self.query))
45
46 @property

UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 10: ordinal not in range(128)
```

The origin of this exception within the exception handling is this section of `splinter/element_list.py`:

```python
def __getitem__(self, index):
if not isinstance(index, int):
return self.first[index]
try:
return super(ElementList, self).__getitem__(index)
except IndexError:
raise ElementDoesNotExist(
'no elements could be found with {0} "{1}"'.format(
self.find_by, self.query))
```

Where the `ElementDoesNotExist` exception's text is a raw string.
This pull request just adds the python `2-3` compatible `u` unicode prefix, which should make the string format operation work either in the case of an ansi python2 raw string and any unicode object.

Should fix #312

Nice work :)