0

i have tried 2 things :- .click() method in javascript and

function simulateClick(onElement) {
  let mouseClick = document.createEvent("MouseEvents");
  mouseClick.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  let cb = onElement;
  let canceled = !cb.dispatchEvent(mouseClick);
}

this simulateclick() function. i want to simulate a click on this website https://contacts.google.com/ on the Create a contact button which comes after clicking on Create contact button, above the Create multiple contacts. please go to the link and look up the issue. so far whatever i have tried, none work. i would realy appreciate an explanation behind this. thank you!

  • `onElement.click()` should work. – Barmar Feb 21 '20 at 18:57
  • I have a content script that clicks on a link, and that's all I do. – Barmar Feb 21 '20 at 18:57
  • 1
    Your going to have difficulties as the "Create Contact" button only appears at certain viewport widths. At smaller ones, a different button is added at the bottom of the screen. If you can overcome this issue, as the buttons are not named for the end user, you just need to invoke the `click()` method. As a suggestion, you might be better off using the Google Contacts API instead of hacking something through JS. – Itanex Feb 21 '20 at 19:04
  • @Barmar that does not work, i have already tried that. – Sanket Chauhan Feb 21 '20 at 19:20
  • @Itanex i didn't get you.. also, i am able to select that element by this query document.querySelectorAll(".z80M1")[4] but cannot click on it using the .click() method. – Sanket Chauhan Feb 21 '20 at 19:23
  • You can try [this approach](https://stackoverflow.com/a/43331339/3930351) – Iván Nokonoko Feb 21 '20 at 19:35
  • It's probably not the problem, but `initMouseEvent` is deprecated. You should initialize the event as shown in https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events – Barmar Feb 21 '20 at 20:24
  • I was going to suggest that you make sure the event bubbles, but that's what the second argument to `initMouseEvent` specifies. – Barmar Feb 21 '20 at 20:25
  • I suspect the problem is that the web page isn't actually using the click event for this, maybe `mousedown`. – Barmar Feb 21 '20 at 20:25
  • These controls both use a ` – Itanex Feb 22 '20 at 00:17
  • @SanketChauhan turns out the button doesn't trigger this functionality. There is a framework that I am not familiar with running on the page. I was under the impression that it was Angular, but after investigating a bit, I do not recognize the HTML attributes. – Itanex Feb 22 '20 at 00:23
  • @Barmar thanks for the help, will try and get back. – Sanket Chauhan Feb 22 '20 at 10:29
  • @Itanex as mentioned in the question i am able to simulate a click on the first **Create contact** and not on the second. the query you specified is for the first one. i appreciate your effort, thank you. but you're unable to understand my problem. :( – Sanket Chauhan Feb 22 '20 at 10:31
  • 1
    @IvánNokonoko worked like a charm, thank you so much! :D – Sanket Chauhan Feb 22 '20 at 10:45
  • @SanketChauhan If it worked, remember to upvote it :) – Iván Nokonoko Feb 22 '20 at 16:09

0 Answers0