-1

So I'm currently creating a test script and one of the scenario is clicking different sets of buttons based on the aria-label.

So a pretty straight forward JS where I do a query selector all and click all the needed buttons but for some reason only the first button gets click.

At first I thought it might have been a race condition so I added a delay

My code:

var share = document.querySelectorAll("[aria-label='SubmitDoc']");
for (let i = 0, len = share.length; i < len; i++) {
  setTimeout(function () {
    console.log(share[i]);
    share[i].click();
  },2000 * i);
}
<div aria-label="SubmitDoc" class="btn-submit" role="button" tabindex="0"></div>    
<div aria-label="SubmitForm" class="btn-submit" role="button" tabindex="0"></div>
<div aria-label="SubmitClass" class="btn-submit" role="button" tabindex="0"></div>
<div aria-label="SubmitDoc" class="btn-submit" role="button" tabindex="0"></div>
<div aria-label="SubmitForm" class="btn-submit" role="button" tabindex="0"></div>
<div aria-label="SubmitClass" class="btn-submit" role="button" tabindex="0"></div>
<div aria-label="SubmitFinal" class="btn-submit" role="button" tabindex="0"></div>
<div aria-label="SubmitFinal" class="btn-submit" role="button" tabindex="0"></div>

Am I missing something?

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • What action happens when the button is clicked? – Taplar Dec 24 '20 at 21:21
  • 1
    What is `share` variable ? and where do you use `submitDoc` ? – Jérôme Teisseire Dec 24 '20 at 21:22
  • Please add the relevant HTML so we can replicate your situation. – Scott Marcus Dec 24 '20 at 21:28
  • please add your full code . this code is imperfect – Arash Hasanzade Dec 24 '20 at 21:36
  • Sorry about that, fixed the code. I forgot to change the variable name when I type it here – Franz Buenaventura Dec 24 '20 at 21:43
  • As I said, please add the relevant HTML to your question, so we can replicate your issue. You should also add the `click` callback and event wiring for the buttons you are testing. – Scott Marcus Dec 24 '20 at 21:45
  • Getting closer, but as mentioned above, we also need the event wiring code for the `click` event. You should always post ALL the relevant code so that we can run it and replicate what is happening. – Scott Marcus Dec 24 '20 at 22:03
  • Will need to get back to you after a few days. Will need to request that code since it is obscured and minified but I'm guessing the issue is not something obvious on my implementation which I thought the most probable case – Franz Buenaventura Dec 24 '20 at 22:07
  • 1
    Well based on what you do have, it seems that it's working as two items are written to the console and you have two matching elements. If the `click` handler code isn't working on one of them, it's got to be something in that handler. – Scott Marcus Dec 24 '20 at 22:09

1 Answers1

-1

You are basically missing a closure. Because you are running a click in a setTimeout, by the time it fires the loop has already run and the value of i is already its maximum value. You can solve this like so:

var submitDoc = document.querySelectorAll("[aria-label='SubmitDoc']")
var doClick = function (node, delay) {
  setTimeout(function () {
    node.click();
  }, delay);
}
for (let i = 0, len = share.length; i < len; i++) {
  doClick(share[i], 2000 * i);
}
andriusain
  • 1,211
  • 10
  • 18