0

I am trying to access the Udemy site and trying to log in via javascript , but I am unfortunately not able to trigger the click action on "log in" link. This is the url. .click() does not seem to be working , when I select the element. Basically only after I trigger the link with a "click", the modal opens up. And since the .click() is not working on the link , I am not able to open the login modal.

    var id = document.getElementById("id_email");
    var password = document.getElementById("id_password");
    var submit = document.getElementById("submit-id-submit");
    id.value = "XXXX@gmail.com";
    password.value="XXXXX";
    submit.click();

The above script works for me only if I manually click the Login link and then execute this script. Like I said I want the click() action on the login button to work , so I can completely automate this login action.

I am struck on how to trigger the click on this element

 document.getElementsByTagName("require-auth")[0].querySelectorAll("a");

Although the element is selected. when I try the click() on the element , I get the following error

  VM2907:1 Uncaught TypeError: document.getElementsByTagName(...)[0].querySelectorAll(...).click is not a function(…)
neer
  • 4,031
  • 6
  • 20
  • 34
user2977259
  • 65
  • 1
  • 11
  • please post what you have tried (html/script) – Sandip - Frontend Developer Aug 17 '16 at 06:09
  • 1
    Possible duplicate of [How to trigger event in JavaScript?](http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript) – pwolaq Aug 17 '16 at 06:10
  • update your question with html and script – Sandip - Frontend Developer Aug 17 '16 at 06:16
  • It is not a duplicate, as I tried that method and this I believe happens to be a special case. Also as you can see , I am able to successfully trigger the form, but not the modal. And If the modal is not triggered first, the form elements are not in the DOM and hence not picked by the selectors. :( – user2977259 Aug 17 '16 at 06:21
  • @user2977259 its not a simple click event that gonna work for you, website's code is an *AngularJS* based code, so you need to follow something like this [**SOAnswer**](http://stackoverflow.com/a/24334176/4763084) – vivekkupadhyay Aug 17 '16 at 06:25
  • @user2977259 I copied code from attached answer, modified it to trigger `click` event, pasted into console and modal opened... – pwolaq Aug 17 '16 at 06:27
  • @pwolaq Can you please post the answer. I am still having difficulty. – user2977259 Aug 17 '16 at 06:35
  • 1
    Thanks @vivekkupadhyay. It works , this works for me angular.element(document.getElementsByTagName("require-auth")[0].querySelectorAll("a")).trigger('click'); P.S: trigger does not work for me not triggerHandler does. I think this depends on the AJS version. – user2977259 Aug 17 '16 at 06:39
  • @pwolaq Thanks for the help – user2977259 Aug 17 '16 at 06:39

1 Answers1

0

Try submit.trigger("click") / $(submit).trigger("click") instead of submit.click();

Buddy
  • 10,874
  • 5
  • 41
  • 58