I need help defining an IF command

Asked

Viewed 49 times

-1

Sometimes when rooting a page I use an error alert appears in the first option, so I have to click on option 2, so I would like to set an IF command,so that when the error alert appears, the script clickasse in the second option, but I do not know how to configure the if. The error code is this:

<div role="alert" aria-live="polite" aria-atomic="true" class="error flash__message"><div class="flash__message-content">You have too low respect to enter this place< data-dismiss="alert" aria-label="alertClose" class="flash__close-button"><span aria-hidden="true">×</span></button></div>

And this is my script:

setInterval(function enter() {
var entrar1 = document.getElementsByClassName('pull-right')[5];
var entrar2 = document.getElementsByClassName('pull-right')[7];
if (AQUI VAI O COMANDO DO ERRO,PORÉM NÃO SEI COLOCAR") {
    entrar2.click();
} else {
    entrar1.click();
}
},1000);

NOTE: I am using tampermonkey to automate the script.

  • In case I just wanted to set the IF in case that alert appears

1 answer

0


You can check if the element exists on the page with:

if( document.querySelectorAll("div.error.flash__message").length ){
   // existe
}else{
   // não existe
}

The document.querySelectorAll("div.error.flash__message").length will count how many div’s have the two classes .error and .flash__message. If there is at least one div on the page with the two classes, the if will be met.

  • thanks, it worked !!

  • Be sure to mark the answer as right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.