SeleniumLibrary icon indicating copy to clipboard operation
SeleniumLibrary copied to clipboard

New Keyword: Get All Texts Of Elements

Open blomqsu1 opened this issue 4 years ago • 1 comments

It would be useful to get all texts of element with certain locator. In my application we have these kind of components, which are not actually list, but I need to verify that it contains all expected items.

<div class='floating-menu'>
   <div class='floating-menu-item'>Item A</div>
   <div class='floating-menu-item'>Item B</div>
</div>

It would be useful if there was a keyword for getting the texts of all elements for a specific locator.

Here is an suggestion (not sure if the syntax is correct, but you should get the idea):

    @keyword
    def get_all_texts_for_elements(self,locator) -> List[str]:
        """Returns a list of texts of WebElements matching the locator
        """
        elements = self.find_elements(locator)
        return [element.text for element in elements]

And it could be used for example like this:

${expected_list} =    Create List     Item A    Item B
${list} =     Get All Texts For Elements     //div[@class='floating-menu-item']
List Should Be Equal    ${list}    ${expected_list}

blomqsu1 avatar Feb 15 '21 08:02 blomqsu1

I agree that this could be useful, now SL will blindly return text from the first element. I am busy with the robocon2021 and with other RF related projects. But if you interested to make a PR, I am happy to review it.

General observations.

  1. Instead of making new keywords, this could be controlled by an argument in the existing keywords.
  2. Most likely other keywords could benefit from similar functionality. So making a generic wrapper could be nice idea.

aaltat avatar Feb 15 '21 08:02 aaltat