0

I have been trying to create a script that favorites all of the items in an etsy store

var buttons = document.getElementsByName('favorite-container');
for(var i = 0; i <= buttons.length; i++)  
buttons[i].click();

However I get this error when I try and run the script in the developer console in Chrome

"Uncaught TypeError: Cannot read property 'click' of undefined at :3:11"

Am I right in thinking that this should work? I don't know of an alternative for running Javascript on a website that I am logged into apart from getting into automation tools like selenium


In response to first comment

<div class="favorite-container" data-listing-id="547704515">
    <button type="button" class="btn-fave done" data-source="casanova-shop-featured" aria-pressed="true">
        <!--icon font and display:none; elements -->
        <span aria-hidden="true" class="icon"></span>
        <span class="screen-reader-only default">
            Favourite
        </span>
        <span class="screen-reader-only done remove">
            Favourited
        </span>
        <span class="ie-fix">&nbsp;</span>
    </button>
</div>
Weefunker
  • 11
  • 1
  • 4
  • Probably because there are no elements that have the name `favorite-container`. – Andrew Li Sep 04 '17 at 00:26
  • You can use getElementsByName here because you aren't using the name attribute anywhere. You're looking for getElementsByClassName. – jswebb Sep 04 '17 at 00:37
  • I have used getElementsByClassName and successfully selected the elements on the page – Weefunker Sep 04 '17 at 00:49
  • However, it will still not let me click them automatically. Could there be something blocking the click event? – Weefunker Sep 04 '17 at 00:49

1 Answers1

2

You are using the Wrong method to get Elements, try document.getElementsByClassName

var buttons = document.getElementsByClassName("favorite-container");
for(var i = 0; i < buttons.length; i++)  {
    buttons[i].click();
}

Or you could use JQuery like this:

$(".favorite-container").each(function(){
    $(this).click();
});
Matheus Cuba
  • 2,068
  • 1
  • 20
  • 31
  • This lists all of the elements but does not click them "(31) [div.favorite-container......................" – Weefunker Sep 04 '17 at 00:46