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
Related videos on Youtube
Author by
AMJ
Updated on June 04, 2022Comments
-
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 over 5 yearsShow 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 over 5 yearsThe OP seems to be interested in solving the problem using Robot FW and not java.
-
A. Kootstra over 5 yearsThis 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.