Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate user click in chrome console

Why can't I trigger or simulate user click using chrome's console in browser? like I have a link on a page I do $('#app .mylink').click() it should go somewhere.

like image 513
Casa Lim Avatar asked Dec 14 '17 03:12

Casa Lim


1 Answers

$ in Chrome's console is an alias for document.querySelector(), except when it's not. If $ is declared in the page, usually by jQuery, $ in the console will point to that instead.

Calling click on a jQuery object representing an a element won't perform the native navigation, but calling click on the native HTMLElement will. If you know the page you're working with uses jQuery, you'll need to retrieve the native HTMLElement from the jQuery object:

$('#app .mylink')[0].click(); // assuming you want to click the first element returned

But if jQuery's not involved, that won't work. Best to be unambiguous:

document.querySelector('#app .mylink').click();
like image 187
AuxTaco Avatar answered Oct 22 '22 01:10

AuxTaco