There is:

setInterval ( function() { document.getElementsByClassName('farm_icon_a').click(); }, 1000 ); 

I receive:

Uncaught TypeError: document.getElementsByClassName (...). Click is not a function at: 3: 56

How to do?

  • document.getElementsByClassName('farm_icon_a') gives you an array of all the elements, scrolls through the array and makes a click on each of them. - Moonvvell
  • 1. Get an element, not an array. 2. Check if the item has click if (item.click) item.click() ; - nick_n_a

3 answers 3

It is better to use querySelector instead of getElementsByClassName , because it caches the result.
getElementsByClassName returns a nodeList, which is a pseudo-array that does not have ordinary array methods.
You need to convert nodeList to an array through Array.prototype.slice.call(nodeList) or Array.from() (this is a new method and not all browsers are supported), go through this array by loop and make click() on each element

 var buttons = Array.prototype.slice.call(document.querySelectorAll('.farm_icon_a')); buttons.forEach(function(button, index) { setTimeout(function() { button.click(); }, index * 100); }); 
  • Fine! But do not tell me how to make a delay on pressing the next button? - Rodion Polyakov
  • By the way, I need the script to re-run - Rodion Polyakov
  • Well in forEach there are 2 parameter index, which is responsible for index. You can use this to create a new amount of time for each iteration. buttons.forEach((button,index) => { setTimeout(() => { button.click(); }, 100 * index); }); Then every trace. the element will be launched with a delay of + 100 ms. And how to run the code again is up to you. You can use setInterval, or you can click. - DimenSi
  • Uncaught SyntaxError: Unexpected token const: ssmaker.ru/16a60f68.png - Rodion Polyakov
  • const and the like, this is the es5 syntax that older browsers don't support. Replace it with var, I corrected my answer. - DimenSi
 setInterval ( function() { var arr = document.getElementsByClassName('farm_icon_a'); for(var i = 0; i < arr.length; i++) { arr[i].click(); } }, 1000 ); 
  • You repeated the mistake of the author. nodeList has no forEach. - DimenSi
  • @DimenSi I have now tested this soton, I have not seen so many tabs in my life) - tCode
 // get button array (Из соседнего ответа) var buttons = Array.prototype.slice.call(document.querySelectorAll('.farm_icon_a')); var clickers = buttons.map(function(button) { return function(){ button.click(); } }); var index = 0; setInterval(function(){ if (index == clickers.length) index = 0; clickers[index](); index++; }, 500);