Selenium IDE Arrays

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?

  1. Selenium runs our JavaScript snippet that returns an empty array.
  2. We then tell our variable myArray to reference (or point to) this empty array.
  3. 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!

49 thoughts on “Selenium IDE Arrays”

  1. 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

    1. christophertangalin@gmail.com

      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!

  2. 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

    1. christophertangalin@gmail.com

      Glad it worked for you, Peter! Javascript arrays have a length property; use it like this:

      ${myArray.length}

      Example:
      CommandEcho
      Target – ${myArray.length}

      Happy automating!

  3. 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

  4. Lammon12392@hotmail.com

    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.

  5. 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

    1. christophertangalin@gmail.com

      You can use Selenium IDE’s for each command. Use this format:

      for each | yourArrayName | iteratorName

      Here is a working example:

      for each command iterate array example

      Was this what you were looking for? Please let me know.

      Best regards,
      Christopher

  6. 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

      1. 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 🙂

        1. christophertangalin@gmail.com

          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?

          1. 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 🙂

        2. christophertangalin@gmail.com

          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.

        3. christophertangalin@gmail.com

          Here is some sample code to get you started:

          times loop example

          This code clicks on the first 3 dropdown menu items. You will need to add the missing steps.

          I hope this helps!

  7. 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.

  8. 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?

    1. christophertangalin@gmail.com

      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

  9. 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.

  10. 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

    1. christophertangalin@gmail.com

      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

  11. 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

    1. christophertangalin@gmail.com

      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.

  12. 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

      1. 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

  13. 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.

  14. 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.

    1. christophertangalin@gmail.com

      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.

  15. 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.

    1. christophertangalin@gmail.com

      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 the href value. In the example below, I’m echoing every link on Google’s homepage.

      Let me know if this helped.

  16. 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!

    1. christophertangalin@gmail.com

      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)?

  17. 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.

Contact Me

If you'd like to reach out, you can find me on LinkedIn.