How to get All Text in robot framework ?

后端 未结 6 1012
走了就别回头了
走了就别回头了 2021-02-06 15:01

Consider the following source code,

    <
6条回答
  •  轮回少年
    2021-02-06 15:27

    You could extend Selenium2Library and write your own keyword for this purpose. Save the following as Selenium2LibraryExt.py

    from Selenium2Library import Selenium2Library
    
    
    class Selenium2LibraryExt(Selenium2Library):
    
        def get_all_texts(self, locator):
            """Returns the text value of elements identified by `locator`.
            See `introduction` for details about locating elements.
            """
            return self._get_all_texts(locator)
    
        def _get_all_texts(self, locator):
            elements = self._element_find(locator, False, True)
            texts = []
            for element in elements:
                if element is not None:
                    texts.append(element.text)
            return texts if texts else None
    

    Then you can use your new Get All Texts keyword in your tests like this:

    *** Settings ***
    library     Selenium2LibraryExt
    
    
    *** Test Cases ***
    Get All Texts Test
      Open Browser    http://www.example.com   chrome
      @{texts}        Get All Texts            css=.name
      Log Many        ${texts}
    

提交回复
热议问题