Iterate through web elements with Selenium and Robot Framework

10,947

You can get all web elements with same class using the Get WebElements keyword, and then you can iterate them with a for loop. Note that I am using the RF 3.1 new for syntax. You can access the text attribute using the extended variable syntax.

${elements}=    Get WebElements    //span[@class='myclass']
FOR    ${element}    IN    @{elements}
    Log    ${element.text}
END

Other option is to use the Get Text keyword inside the loop, you can pass the web element variable as a locator.

${elements}=    Get WebElements    //span[@class='myclass']
FOR    ${element}    IN    @{elements}
    ${text}=    Get Text    ${element}
    Log    ${text}
END
Share:
10,947

Related videos on Youtube

AMJ
Author by

AMJ

Updated on June 04, 2022

Comments

  • AMJ
    AMJ almost 2 years

    On the web page there are many elements with the same span class name. And I'm able to get the value of the first element with keyword "Get Text class: ...".

    But I just can not figure it out how to iterate and get the values of all those same span class names. Any ideas?

    I know how to iterate e.g. a text file with Python, but I'm not yet familiar enough with Selenium and RFW.

    • Todor Minakov
      Todor Minakov over 5 years
      Show a sample of the code you tried with; if you don't have such and don't know where to start from, the "For loops" section in the user guide is a good start
  • Bence Kaulics
    Bence Kaulics over 5 years
    The OP seems to be interested in solving the problem using Robot FW and not java.
  • A. Kootstra
    A. Kootstra over 5 years
    This would mean doing a separate call to the browser for each element. Though it would technically work, I'd consider it a very slow approach. Using the Get Webelements approach fetches all of the elements in memory and thus only 1 interaction with the browser is needed.