<div class = "turbolink_scroller has-cart" id="container"> <article> <div class="inner-article"> <h1> <a class="name-link" href="/shop/bags/v08oxnl69/vwl3p5cav">Backpack</a> </h1> <p> <a class="name-link" href="/shop/bags/v08oxnl69/vwl3p5cav">Red</a> </p> </div> </article> </div> 

Good day to all. This is a code snippet of some page. I need to open this link through the browser console ( <a> in <p> and <a> in <h1> are the same links.) That is, the site has two buttons (Backpack and Red), I need to simulate in the console user click on any of these buttons. Already clambered many resources, but was not able to achieve the desired result. If someone knows how to solve this problem, please share your ideas with me. Thank you for your attention.

1 answer 1

If simply then HTMLElement.click ()

 document.querySelecor(".name-link").click() 

If absolutely universal,

 function callEvent (element, event) { if(document.createEventObject) { element.fireEvent("on" + event); } else { var evt = document.createEvent("HTMLEvents"); evt.initEvent(event, false, true); element.dispatchEvent(evt); } } callEvent(document.querySelecor(".name-link"), "click"); // или любое другое событие