Working with arrays in Selenium IDE can be a bit confusing at first but don’t worry. I’ll show you how to create an array, add elements to an array, and access elements within an array.
Create an Array in Selenium IDE
To create an array, use this: Command = execute script
| Target = return[]
| Value = myArray
-
execute script
tells Selenium IDE that we want to run a JavaScript snippet. -
return[]
is the JavaScript snippet that we want to run. -
myArray
is the variable where our return value will be stored.
It works! But how does it work?
- Selenium runs our JavaScript snippet that returns an empty array.
- We then tell our variable
myArray
to reference (or point to) this empty array. - myArray is now an array!
Congratulations, you now have to power to create arrays in Selenium IDE! Please use this power wisely.
Learn more about the execute script command here.
Add Elements to an Array in Selenium IDE
There are several ways to add elements to an array. I’ll show you how to add elements using execute script
, array.concat
, and the store
command.
Adding array elements using execute script
To initialize an array and add elements to it using execute script
, use this: Command = execute script
| Target = return [1,2,3]
OR return ["Element 1", "Element 2", "Element 3"]
| Value = myArray
This method is perfect for creating arrays that do not change. In a single command, it creates an array and adds elements to it.
Adding array elements using array.concat
To add an element to an array using array.concat
, use this: Command = execute script
| Target = return ${myArray}.concat("Value to store")
| Value = myArray
Each new element is inserted at the end of the array. If you are planning to add additional elements later in your program, this method is ideal.
Adding array elements using the store command
To add an element to an array using the store
command, use this: Command = store
| Target = The element I want to add
| Value = myArray[0]
. To add multiple elements to an array you can use a Control Flow command to create a loop in order to increment the index value.
I prefer not to use this method because at the time of writing it does not work with Selenium IDE’s forEach Control Flow command. You’ll need to create your own custom loop to iterate over the array.
Read Elements in an Array in Selenium IDE
There are several ways to read the elements in an array. I’ll show you how to read elements using the for each command and using the element’s index.
Reading elements in an array using the for each command
To read elements using the for each
command, use this: Command = for each
| Target = myArray
| Value = iterator
Here is an example of its use:
In this example, I am iterating through an array called arrEmployees and writing each element value to a Google form.
Reading elements in an array using the element’s index
To read elements using the element’s index in an array, simply include the index of the element when calling the array.
Here is an example:
In this example, I am echoing the value of the 2nd element in the array: “orange”.
That’s all for now folks! You now possess invaluable knowledge that grants the power to create, edit, and read arrays and their elements. Thanks for reading!
How do I add a variable say $newFruit to the array? Where $newFruit is defined somewhere as Watermelon for example. Want to add value of the variable. TIA
Hi Peter. I’d do it like this:
Command = execute script | Target = return ${myArray}.concat(${newFruit}) | Value = myArray
You can see a full working example here:
Let me know if it works for you!
Super that was it. Don’t know why I could no find that anywhere.
Also how do I get the length of myArray?
Say print it after the last echo above(I will be using in a loop)
Thanks Kindly
Glad it worked for you, Peter! Javascript arrays have a length property; use it like this:
${myArray.length}
Example:
Command – Echo
Target – ${myArray.length}
Happy automating!
Thanks Christopher, I tried so many combinations. Is there a resource I can use that has this type of info on selenium ide? I looked every where for the length and could not find an example. Thanks again Peter
Unfortunately, there aren’t many resources available for some of the questions you might have; that’s why I wrote this article. Stack Overflow and Selenium Slack can be helpful.
https://www.selenium.dev/support/
I’m really impressed together with your writing skills as well as with the format in your blog. Is that this a paid topic or did you customize it your self? Either way stay up the excellent quality writing, it’s uncommon to peer a great weblog like this one these days.
Thanks for the kind words. I wrote it myself. Cheers!
Hi,
How do I print the values of the array dynamically by iterating through them rather than printing them via hardcoded way (echo ${myNewArray[1]})
Thanks
You can use Selenium IDE’s for each command. Use this format:
for each | yourArrayName | iteratorName
Here is a working example:
Was this what you were looking for? Please let me know.
Best regards,
Christopher
Hi Christopher,
I have an issue that I cannot solve on my own. In my application under test there is a dropdown list with 581 value (time zones) which I select by index. I want to make a loop that goes through all of them. Clicks on dropdown, selects value clicks on it again and then hit “Apply” button. This would really help me out since hardcoding 581 values is not an option for me:))
IDe generated code below:
1.run Login script
2.click on linkText=Config OK
3.click on css=#mnuSystem .col-9 OK
4.click on id=systemTimeZone OK
5.select on id=systemTimeZone with value index=0 OK
6.click on id=systemTimeZone OK
7. click on css=.btn OK
Hi Vytas,
You can use the times command to create a loop that will run as many times as you specify.
You can learn how to use it here: https://www.selenium.dev/selenium-ide/docs/en/introduction/control-flow#times-selenium-ide-docs-en-api-commands-times
Let me know if this solves your problem.
Best regards,
Christopher
Yeah, I know about the times loop. What I don’t know how to do with this code
4.click on id=systemTimeZone OK
5.select on id=systemTimeZone with value index=0 OK
6.click on id=systemTimeZone OK
7. click on css=.btn OK
I need to select index=0 click on the value and click “Apply” and I need to do this +1 for every run until I reach 581 index 🙂
You are able to click the first one but not the second, third, fourth, etc?
Or you need help writing code that does steps 4 – 7?
Can you share your .side file?
Yeah I need some help with 4-7 steps how to automate them so that every time it would go up by 1 index starting from 0 and going up to 581.
Not much of a help from side file since it’s only those steps that I wrote before that needs to be encapsulated in a loop 🙂
I would create another variable that starts at 0, and increments each time you run the loop.
Then do something like select on id=systemTimeZone with value index=${counterVariable} instead of using the static value.
Here is some sample code to get you started:
This code clicks on the first 3 dropdown menu items. You will need to add the missing steps.
I hope this helps!
Sure that helped :”)
Hi there, You have done an excellent job. I will definitely digg it and personally suggest to my friends.
I’m sure they’ll be benefited from this web site.
Appreciate it, glad you found it helpful!
Hi Christopher, Thanks for the awesome article.
Do you know how I can access an array index via variable?
I tried some combinations of : ${MyArray[index]} and ${MyArray[${index}]} but it did not work.
What would be the proper format for that?
Hi Alex, sorry for the late reply. Do you want to access the variable stored at a specific index of your array OR are you trying to access the current iterator value? If you want, you could send me a copy of your .side file and I could take a look.
Cheers,
Christopher
Hi Christopher, Thanks for the awesome article.
I have created a program which inputs value from an array to a web form and echoes the returned data in the log
but since i have used it in a while loop the log is getting filled with unneeded stuff in between the required stuff
how do i supress some of the lines of code from writing to the log.
I haven’t actually tried that myself so I’m not too sure. Perhaps they’ll know the answer in the Slack Channel found here: https://www.selenium.dev/support/
Let me know how it goes, cheers!
it will work like this : ${MyArray}[${index}]
Hi Christopher, I have the same problem what Alex. Instead of an index number, I would like to put in this place variable. I tried a couple of combination but it still does not work as I want.
COMMAND TARGET VALUE
store Green varColour_1
store Yellow varColour_2
execute script return[${varColour_1}, arrayColour
${varColour_2}]
———————————————————————
store ${arrayColour[0]} varY
echo ${varY}
Log
echo: Green
———————————————————————
store 0 index
store ${arrayColour[${index}]} varX
echo ${varX}
Log
echo: ${arrayColour[${index}]} -> in here I would like to see Green
Hi Nita. I still haven’t figured out how to echo specific array values using another variable as an index. However, I do have a workaround that involves adding an extra step: I use “execute script” to grab the array value at the index specified, then I echo the variable that holds the grabbed value. Please let me know if this workaround solves your problem.
Best regards,
Christopher
Thanks! That helped me a lot and solve my next problems!
Glad I could help, cheers!
I’ve been trying to solve this issue for 2 days. Thank you!
Hi i am glad to find this website but still cant figure it out my problem. Is it possible to call one variable with another? I have list of variable values named like 1,2,3,4,5…. . those variables are actually links. I created a variable as a counter add 1 each of the time and i want to use this to get my links. but i couldnt achive, any idea how to make it? asked it with detail here : https://stackoverflow.com/questions/67636260/selenium-ide-3-17-defining-nested-variable
Maybe it’s a better idea to create an array with the links, then add a “for each” loop to iterate through the array and open up each link.
Here’s some example code:
Let me know if this solves your problem.
bonsoir, je fais des tests sur CRM , j’ai des tableaux dans lesquelles je dois saisir des valeurs. je vais essayer tes méthodes pour voir et je te reviens
I can’t seem to get rid of a simple alert that appears on my page.
i need help please.
Please share a screenshot here and your code if possible, thanks.
command TARGER
1. choose ok on next confirmation
2. click–> id={920432ec-cb24-5471-9866-dc7d14b917d4}_content
3. assert alert Les comptes du Client s�lectionn� sont d�biteurs. Cliquer sur le lien “D�tails” pour avoir le d�tails des dettes de ce client !
4. webdriver choose ok on visible confirmation
how to add values in two different cells of my table
Maybe you can click OR click at the xpath of the cell and use send keys to type the values into each cell. I don’t think you can use this for Google Sheets though.
https://www.selenium.dev/selenium-ide/docs/en/api/commands
I want to add the content of a variable and an integer, how to do
Good afternoon. Please tell me how to catch the last created element using the Selenium IDE:
type
name = name
New Division
clickAndWait
css = # form-add> input.btn.btn-primary
assertValue
// input [@ name = ’roles [362] [name]’]
New Division
When adding a new element, the locator for it is created on the basis of +1
that is, only the numerical value changes.
For xpath it looks like this: // input [@ name = ’roles [362] [name]’] the next element created will have xpath // input [@ name = ’roles [363] [name]’]
Actually the question is: how to catch the last created element, which is the element with the maximum chill value?
I would be very grateful if you can help me figure it out.
Good afternoon. Please tell me how to catch the last created element using the Selenium IDE:
type
name = name
New Division
clickAndWait
css = # form-add> input.btn.btn-primary
assertValue
// input [@ name = ’roles [362] [name]’]
New Division
When adding a new element, the locator for it is created on the basis of +1
that is, only the numerical value changes.
For xpath it looks like this: // input [@ name = ’roles [362] [name]’] the next element created will have xpath // input [@ name = ’roles [363] [name]’]
Actually the question is: how to catch the last created element, which is the element with the maximum chill value?
I would be very grateful if you can help me figure it out.
Hi Tim, are you trying to capture the element value or the element index?
Perhaps a counter could be used to keep track of the last created element?
I’m not sure that I understand your problem correctly though. Would you mind sharing a screenshot of the code in Selenium IDE? You can also send your .ide file to christophertangalin@gmail.com and I will have a look at it.
Is it possible to loop through one or more links on a page? The ID’s are never the same but I guess that’s another issue.
Yes, it is possible!
Here’s one way to loop through every single link on a page using the
document.links
property. You can access each URL using thehref
value. In the example below, I’m echoing every link on Google’s homepage.Let me know if this helped.
That was a lot of help! Thank you so much.
Glad I could help, cheers!
Hi! nice article! I would like to ask about a problem I am facing: I need to search for an especific element in a column of a table in a web page, and then access another element in the same row but different column. Is there a way to do that? I had found the command “storeElementIndex”, but it seems to be deprecated. Thanks in advance for your help!
Hi Ileana,
You can use locators to find a specific element on a page. Read more about locators here:
https://www.guru99.com/locators-in-selenium-ide.html
https://www.javatpoint.com/selenium-ide-locating-strategies
Once you’ve located the element you would have to figure out a way to access the other element on the same row. How do you decide which element should be accessed? Are there any rules to determine the final element to be accessed (e.g. the element three columns to the left of the initially searched-for element)?
Good morning,
Is it possible to have a multidimensional array in selenium ide? I want to pass multiple values into a page on a web application and then do an assert against a calculation on the next page. So for example on page 1, I might want to pass 1 and 2, and assert that on the next page, the answer is 3. I’ll have hundreds of these combinations so I’d like my array to have rows of aval1, Val2 and then the desired result.