2

I want to click on this element.

<a href="/game.php?vil=9&amp;screen=settings&amp;action=sitter_login&amp;player=305&amp;h=417c13fb" target="_blank" class="btn">ini</a>

There are other elements, just like that where only the href changes. I can correctly find the element I want. But cannot click it... I tried the usual click() but didn't work:

document.getElementsByClassName("btn")[1].click()

Any help?

I tried this, it opens the same window, but I want to open in a new tab. I also put the internet option to open tabs with popups:

b=document.getElementsByClassName("btn")[1].href;
window.location.assign(b, '_blank');

How to open in new tab? I tried window.open() but never got to make it work:

window.open(b, '_blank');
Ruikp
  • 57
  • 6
  • 2
    I think all you try is OK. Just, browsers tends to block to open new tabs and usually adds a _small_ warning on the top of the window. Check the permissions for your site about to open new tabs. Usually there is a special icon on the address bar. – Syscall Feb 17 '18 at 12:59
  • Your last example will work in response to a user-generated event, but see @Syscall's point above. You're probably being blocked by the browser's popup blocker. – T.J. Crowder Feb 17 '18 at 13:02
  • How can I check that? I'm using chrome – Ruikp Feb 17 '18 at 14:20
  • Got it. Thank you @Syscall – Ruikp Mar 03 '18 at 13:32

2 Answers2

0

You can find the answer here How can I trigger a JavaScript event click. If you execute the .click() function, you trigger all the listener that are bind on the 'click' ( .onClick ). Try to listen for the click event and then execute the .click() function

Marco Predari
  • 93
  • 2
  • 7
  • 1
    If the answer is on SO, don't post an answer. Instead, when you have enough rep, post a comment pointing to the answer; and later than that, when you have enough rep, vote to close as a duplicate. – T.J. Crowder Feb 17 '18 at 13:01
  • @MarcoPredari , its not my webpage, I can't do it. – Ruikp Feb 17 '18 at 13:11
0

I'm not really sure i understand the question all the way. But when you actually click a link all other JavaScripts stops loading and you get to that webpage. The window.open is right. But you have to prevent the Url to actually redirect you to the website. You can do this by using preventDefault on the eventobject that fires on your click. Hope this helps

let myLink = document.getElementsByClassName("btn")[1];

myLink.addEventListener('click', function newWindow(event) {
  event.preventDefault();
  window.open(this.href, '_blank')
}) 
  myLink.click();