How to get All Text in robot framework ?

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

Consider the following source code,

    <
相关标签:
6条回答
  • 2021-02-06 15:15

    Though the top-rated answer is fully working - and the most xpath-ish :), let me add an option I don't see proposed yet - using the Get Webelements keyword. E.g.:

    @{locators}=     Get Webelements    xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer']//span
    ${result}=       Create List
    
    :FOR   ${locator}   in    @{locators}
    \       ${name}=    Get Text    ${locator}
    \       Append To List  ${result}  ${name}
    

    It'll generate and return a list of all matching elements, on which you just iterate on. It might be a tad faster than xpath's [index] reference because the dom is evaluated once - but don't hold me accountable if that's not fully true :)

    0 讨论(0)
  • 2021-02-06 15:21

    @Velapanur

    I had another similar requirement where in i have to enter the texts into textareas in a page. The below i wrote with the idea in reference to what Todor had suggested, and it worked. Many thanks to Todor, Velapanur

    @{Texts}=    Get WebElements    ${AllTextboxes}
    
        :FOR    ${EachTextarea}    in    @{Texts}
    
    \    Input Text    ${EachTextarea}    ${RandomTextdata}
    
    0 讨论(0)
  • 2021-02-06 15:27

    You could iterate over the elements as below:

    ${xpath}=    Set Variable    //div[@id='groupContainer']//li[@class='contactNameItemContainer']//span
    ${count}=    Get Matching Xpath Count    ${xpath}
    ${names}=    Create List
    :FOR    ${i}    IN RANGE    1    ${count} + 1
    \    ${name}=    Get Text    xpath=(${xpath})[${i}]
    \    Append To List    ${names}    ${name}
    

    This works but is rather slow when there are many matches.

    0 讨论(0)
  • 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}
    
    0 讨论(0)
  • 2021-02-06 15:31

    Get Text will return content of the first element that matches the locator. When using XPATH you can specify the index of the element you want to get, like this:

    ${name1}    Get Text    xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer'][0]//span
    ${name2}    Get Text    xpath=//div[@id='groupContainer']//li[@class='contactNameItemContainer'][1]//span
    @{names}    Create List    ${name1}    ${name2}
    
    0 讨论(0)
  • 2021-02-06 15:40

    Here is the logic to get all elements in Java. You can adopt it to your need.

    You have to use findElements() and not findElement() to get all elements.

    List<WebElement> items = driver.findElements(By.cssSelector("ul#GroupContactListWrapper div.contactNameItem"));
    
    foreach(item in items){
       System.out.println(item.getText();
    }
    

    If you want a particular element from the list you can use items.get(1)

    0 讨论(0)
提交回复
热议问题