How to click multiple buttons with the same answer

I have a page with these two types of buttons:

<button type="button" class="btn btn-default">This is not me</button>
<button type="button" class="emphasize btn btn-default">This is me</button>

If I open the console and launch this:

var inputs = document.getElementsByClassName("btn btn-default");
for(var i=0; i<inputs.length;i++)
{
inputs[i].click();
}

it behaves like crazy: it clicks the first button of the first type, then the second of the second type, and then it skips one button of the first type and one of the second type. And it restarts again with the remaining buttons.

How can I make it click instead all and only the buttons with the “This is not me” tag?

So at the end your approach is wrong at all.
Why do you want to click a button programmatically? As you should now which function is called when a button is clicked, you should directly call the function instead.

1 Like

Because I’m the user of the page, not the creator.
It’s an Academia page that should contain the mentions to my name, but instead contains 7.000 mentions to a homonym. Either I click 7.000 times “This is not me” or I find a way to do it automatically via the Chrome console.
So, I don’t know which function is called (could I find it out?).

I can see the buttons are contained in this div:

<div class="wp-workCard_item wp-workCard--mentionsSelector btnContainer">
<button type="button" class="emphasize btn btn-default">This is me</button>
<button type="button" class="btn btn-default">This is not me</button>
</div>

querySelectorAll(".btn-default:not(.emphasize)")

Thank you. This is the effect:
Uncaught ReferenceError: querySelectorAll is not defined

@gregoriograsselli

I think m_hutley was presuming you would know to call that method on document.

document.querySelectorAll(".btn-default:not(.emphasize)")
1 Like

Thank you. No, I’m utterly ignorant. Should I run this then?

document.querySelectorAll(".btn-default:not(.emphasize)")
for(var i=0; i<inputs.length;i++)
{ inputs[i].click(); }

You could try this.

var inputs = document.querySelectorAll(".btn-default:not(.emphasize)")

for (var i = 0, len = inputs.length; i < len; i++) {
  inputs[i].click()
}

Brilliant, it works!!!
Thank you very much.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.