Arnie Cunningham Arnie Cunningham - 25 days ago 10
Javascript Question

How can I use javascript to click these elements?

When you sign up to pinterest, it asks you to select 5 topics to build your home feed. I'd like to execute javascript to click 1 of these buttons.

I right clicked on the area I want clicked and clicked "inspect" and I see this code come up:



<div class="interestImage" style="background-image: url(https://s-media-cache-ak0.pinimg.com/400x/a5/6d/90/a56d900e41ce2a6205fa2d830419e387.jpg)" data-element-type="211" data-aux="{ &quot;interest&quot;: &quot;hairstyles&quot; }"></div>





And here is what the page looks like:

I right clicked on the hairstyles one

Any idea if I can use javascript for this?

Answer
document.getElementsByClassName("interestImage")[0].click();

This will only work if this element is the first/only element on the page with this class.

Otherwise, you will need to replace the 0 inside the [] with the number that corresponds to the index of that element in the array of elements with that class. For example, it that's the 3rd element on the page with that class, replace the 0 with a 2.