Skip to content

Commit da3f155

Browse files
kakooloukiakakooloukia
and
kakooloukia
authored
Added optional first_only parameter to Get Text (#402)
* Be able to get the text from all elements matching one locator * Default behavior not changed Co-authored-by: kakooloukia <[email protected]>
1 parent 5f5e1f9 commit da3f155

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

AppiumLibrary/keywords/_element.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,18 @@ def get_element_rect(self, locator):
467467
self._info("Element '%s' rect: %s " % (locator, element_rect))
468468
return element_rect
469469

470-
def get_text(self, locator):
470+
def get_text(self, locator, first_only: bool = True):
471471
"""Get element text (for hybrid and mobile browser use `xpath` locator, others might cause problem)
472472
473-
Example:
473+
first_only parameter allow to get the text from the 1st match (Default) or a list of text from all match.
474474
475-
| ${text} | Get Text | //*[contains(@text,'foo')] |
475+
Example:
476+
| ${text} | Get Text | //*[contains(@text,'foo')] | |
477+
| @{text} | Get Text | //*[contains(@text,'foo')] | ${False} |
476478
477479
New in AppiumLibrary 1.4.
478480
"""
479-
text = self._get_text(locator)
481+
text = self._get_text(locator, first_only)
480482
self._info("Element '%s' text is '%s' " % (locator, text))
481483
return text
482484

@@ -667,10 +669,12 @@ def _element_find_by_text(self, text, exact_match=False):
667669
_xpath = u'//*[contains(@{},"{}")]'.format('text', text)
668670
return self._element_find(_xpath, True, True)
669671

670-
def _get_text(self, locator):
671-
element = self._element_find(locator, True, True)
672+
def _get_text(self, locator, first_only: bool = True):
673+
element = self._element_find(locator, first_only, True)
672674
if element is not None:
673-
return element.text
675+
if first_only:
676+
return element.text
677+
return [el.text for el in element]
674678
return None
675679

676680
def _is_text_present(self, text):

0 commit comments

Comments
 (0)