This question already has an answer here:

My requirement is actually to click on the option in the dropdown one by one and then click search i.e. I will first select the first option then search , then again go back to the second option and click search. This is to be continued till the last option

marked as duplicate by Alexey R., Nitin Rastogi, Bharat Mane, trashpanda, NarendraC Jun 25 '18 at 8:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I've voted to close this as it looks like a duplicate of your other question (sqa.stackexchange.com/q/34377/9208). If you can edit your question enough for it to be different, then it might be re-opened. – trashpanda Jun 25 '18 at 12:16

In if condition, you have already written click() again outside of loop again why we need click and no need to use List for single search button, you can give direct search xpath for it, if it has to continue till last open, please remove break in if condition.

  • There are three buttons on the same page with same xpaths, so using list to store them and then get it by index, click is for selecting that option from the dropdown, and other click is to search – ravenclaw_93 Jun 23 '18 at 13:36
  • removing break also didnt help – ravenclaw_93 Jun 23 '18 at 13:37
  • keep search button click withing for loop, otherwise ur search button gets selected only one item after completing complete for loop iteration. – user2762008 Jun 23 '18 at 15:45
  • I am getting StaleElementReferenceException for the code now – ravenclaw_93 Jun 23 '18 at 16:19

The problem with StaleElementReferenceException is usually that you look up elements on a page, hold them in a variable and then do some interaction that causes the page to change or reload. It seems you are doing exactly this: the list variable is initialized first, then you loop through it and then you click a search button multiple times, which probably updates the page. This makes the list variable invalid and will cause the exception if you try to interact with any of it's elements.

What might work in this case is if you do the following:

  • Hold a variable of the amount of items in the list (currently a, it's recommended to have more descriptive variable names)
  • Loop through all values between 0 (inclusive) and a with index i.
  • Within each iteration, initialize list again and take element i
  • Proceed to do the same as what you are currently already doing inside the loop with element e
  • needed some help with this - "Within each iteration, initialize list again and take element i", with ref to my code – ravenclaw_93 Jun 24 '18 at 7:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.