最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript .click on DIV not working, why? - Stack Overflow

programmeradmin9浏览0评论

I'm doing an app using JavaScript to collect contacts from Google Contacts. What I need to do is navigate to this site, click on a DIV and extract the HTML source.

Problem is, when I manually click on the DIV, the website has the expected behaviour. But when I call click on the same DIV programatically, nothing happens.

Here is the DIV in question :

<div id=":se" class="tk3N6e-LgbsSe VIpgJd-TzA9Ye-eEGnhe Ycq2Ve tk3N6e-LgbsSe-roVxwc tk3N6e-LgbsSe-n2to0e tk3N6e-LgbsSe-vhaaFf-LK5yu ipG21e" role="button" tabindex="0" data-tooltip="Next" aria-label="Next" style="user-select: none;">
<span class="Fj0vqf" aria-hidden="true">&nbsp;</span>
<img class="P1rG1b tk3N6e-LgbsSe-RJLb9c" src="images/cleardot.gif" alt="">
</div>

To achieve the task, I'm using this JS :

document.getElementsByClassName('tk3N6e-LgbsSe')[7].click();

Although the selector above return the correct node, when I call .click(), nothing happens. What am I missing here?

Below is the site image with the DIV in question highlighted:

I'm doing an app using JavaScript to collect contacts from Google Contacts. What I need to do is navigate to this site, click on a DIV and extract the HTML source.

Problem is, when I manually click on the DIV, the website has the expected behaviour. But when I call click on the same DIV programatically, nothing happens.

Here is the DIV in question :

<div id=":se" class="tk3N6e-LgbsSe VIpgJd-TzA9Ye-eEGnhe Ycq2Ve tk3N6e-LgbsSe-roVxwc tk3N6e-LgbsSe-n2to0e tk3N6e-LgbsSe-vhaaFf-LK5yu ipG21e" role="button" tabindex="0" data-tooltip="Next" aria-label="Next" style="user-select: none;">
<span class="Fj0vqf" aria-hidden="true">&nbsp;</span>
<img class="P1rG1b tk3N6e-LgbsSe-RJLb9c" src="images/cleardot.gif" alt="">
</div>

To achieve the task, I'm using this JS :

document.getElementsByClassName('tk3N6e-LgbsSe')[7].click();

Although the selector above return the correct node, when I call .click(), nothing happens. What am I missing here?

Below is the site image with the DIV in question highlighted:

Share Improve this question edited Jul 12, 2019 at 22:09 brasofilo 26.1k15 gold badges93 silver badges186 bronze badges asked Apr 10, 2018 at 12:38 delphirulesdelphirules 7,43818 gold badges69 silver badges130 bronze badges 2
  • 1 where do you have defined your click event? can you provide an example? – N. Ivanov Commented Apr 10, 2018 at 12:40
  • 1 It's not my event, it's the event on Google Contacts : google./contacts/u/0/?cplus=0#contacts – delphirules Commented Apr 10, 2018 at 12:42
Add a ment  | 

2 Answers 2

Reset to default 10

The following worked for me, had to do a mouseup after a mousedown.

document.getElementsByClassName('tk3N6e-LgbsSe')[7].dispatchEvent(new MouseEvent('mousedown'))
document.getElementsByClassName('tk3N6e-LgbsSe')[7].dispatchEvent(new MouseEvent('mouseup'))

The click is bound to the TR element.

document.querySelectorAll("tr.K9ln3e")[4].click()

And than I saw that it is not the person you are clicking on, but the arrow. Funny thing is I do not have enough contacts in gmail to have the arrow so I can only assume this will work

var dv = document.getElementsByClassName('tk3N6e-LgbsSe')[7]
var clickEvent = new MouseEvent('mousedown', {
  view: window,
  bubbles: true,
  cancelable: true
});
dv.dispatchEvent(clickEvent);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论