1

I'm very new to JavaScript, but I'm trying to automate a series of clicks on a webpage. I've managed to successfully do this via Selenium with Python, which is my preferred language, however, I've learned that this can be done from the console in 'Inspect Element' via JS.

So far, I have located the element via the document.GetElementByClassName, however when I simply tag on the .click() and press enter, nothing actually happens. I've been watching multiple instructional videos, and in each of them as soon as this is done the click is performed. For some reason, this doesn't happen for me.

The button I am trying to click has the HTML:

<button class="btn-standard call-to-action">Search</button>

When I then enter the following,

document.getElementsByClassName("btn-standard call-to-action");

the console returns a HTML collection, which contains the button. Using the click() method just returns undefined in the console, with no visible action to the button.

I'm aware that this is an awkwardly phrased question, and it's solution may be blatantly obvious to the users here, but I'm at a loss as to why nothing happens. I've also performed the click with the index [0] on the getElement, still to no avail. If it matters, I'm using safari (with the JS enabled).

Thank you very much in advance.

EDIT:

The page I'm trying to access is behind a log-in screen so requires a username and password. However, the same issue is present with the log-in button. The link is: https://www.easports.com/fifa/ultimate-team/web-app/

clh98
  • 11
  • 2
  • 1
    Have you included [jQuery](https://jquery.com) in your code? `.click()` is part of it. For a pure javascript solution see: https://stackoverflow.com/questions/19655189/javascript-click-event-listener-on-class?answertab=active#tab-top – KIKO Software May 03 '20 at 20:55
  • @KIKOSoftware Actually `.click()` is part of available Web API's and it is available within javascript. You can read [this](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click) for more info. – SMAKSS May 03 '20 at 21:13
  • @clh98 Can you provide a live active link to it? – SMAKSS May 03 '20 at 21:24
  • I've added one in the edit, thank you for your time. – clh98 May 03 '20 at 21:26
  • 1
    I've found an old question with a solution to clicking the log-in button, at https://stackoverflow.com/questions/57333840/click-function-is-not-working-on-a-button – clh98 May 03 '20 at 21:50
  • @clh98 Yea, this is exactly the same question though. – SMAKSS May 03 '20 at 21:58
  • Does this answer your question? [Click function is not working on a button](https://stackoverflow.com/questions/57333840/click-function-is-not-working-on-a-button) – SMAKSS May 03 '20 at 21:59
  • @SMAKSS, I'm aware, I've only now found it. Also, the method seems to not work past the log-in page. – clh98 May 03 '20 at 22:00
  • @clh98 Just copy-paste it, it's working just fine. – SMAKSS May 03 '20 at 22:01

1 Answers1

0

Answer found at: Click function is not working on a button

var targetNode = document.getElementsByTagName("button")[0];
if (targetNode) {
    //--- Simulate a natural mouse-click sequence.
    triggerMouseEvent (targetNode, "mouseover");
    triggerMouseEvent (targetNode, "mousedown");
    triggerMouseEvent (targetNode, "mouseup");
    triggerMouseEvent (targetNode, "click");
}
else
    console.log ("*** Target node not found!");

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}
clh98
  • 11
  • 2