Skip to content

Added optional first_only parameter to Get Text #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions AppiumLibrary/keywords/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,18 @@ def get_element_rect(self, locator):
self._info("Element '%s' rect: %s " % (locator, element_rect))
return element_rect

def get_text(self, locator):
def get_text(self, locator, first_only: bool = True):
"""Get element text (for hybrid and mobile browser use `xpath` locator, others might cause problem)

Example:
first_only parameter allow to get the text from the 1st match (Default) or a list of text from all match.

| ${text} | Get Text | //*[contains(@text,'foo')] |
Example:
| ${text} | Get Text | //*[contains(@text,'foo')] | |
| @{text} | Get Text | //*[contains(@text,'foo')] | ${False} |

New in AppiumLibrary 1.4.
"""
text = self._get_text(locator)
text = self._get_text(locator, first_only)
self._info("Element '%s' text is '%s' " % (locator, text))
return text

Expand Down Expand Up @@ -670,10 +672,12 @@ def _element_find_by_text(self, text, exact_match=False):
_xpath = u'//*[contains(@{},"{}")]'.format('text', text)
return self._element_find(_xpath, True, True)

def _get_text(self, locator):
element = self._element_find(locator, True, True)
def _get_text(self, locator, first_only: bool = True):
element = self._element_find(locator, first_only, True)
if element is not None:
return element.text
if first_only:
return element.text
return [el.text for el in element]
return None

def _is_text_present(self, text):
Expand Down