0

I'm very new on web developing. I want to ask on how Can I automatically Click the second href, that contains the word "login"

This is the code that I tried

document.querySelector('a[href*="/a/login.php?"]')[2].click();

2 Answers2

0

You need querySelectorAll for this and since it's an array-like collection it starts with a zero index, which means you need to take [1] instead of [2]

document.querySelectorAll('a[href*="/a/login.php?"]')[1].click();

Dom
  • 734
  • 3
  • 8
0

function querySelector() returning only 1st element. You need to use .querySelectorAll() instead

const links = document.querySelectorAll('.container a');
links[1].click()
<div class="container">
    <a href="#">link1</a>
    <a href="#">link2</a>
  </div>
qRi
  • 1
  • 1