Hover over an element using JavaScript? - Stack Overflow

Note: I want to hover the element not to trigger an event on hover. (a kind of automated hover using Ja

Note: I want to hover the element not to trigger an event on hover. (a kind of automated hover using Javascript)

I want to react on Facebook post's ments on my wall randomly by running JS in chrome console.

For which I have written this line to test the 1st index in the list:

document.querySelectorAll('._6ijk a._6a-y')[0].click() 

When I hover over an element manually and use this code then It works, for which the selector is:

document.querySelector("[aria-label=Love]").click()

but before hovering there is no such element for "[aria-label=Love]"

But as all other reactions except like button appear on hovering on the like button which is running me into the trouble. Is there any good solution for tackling this scenario?

Note: I want to hover the element not to trigger an event on hover. (a kind of automated hover using Javascript)

I want to react on Facebook post's ments on my wall randomly by running JS in chrome console.

For which I have written this line to test the 1st index in the list:

document.querySelectorAll('._6ijk a._6a-y')[0].click() 

When I hover over an element manually and use this code then It works, for which the selector is:

document.querySelector("[aria-label=Love]").click()

but before hovering there is no such element for "[aria-label=Love]"

But as all other reactions except like button appear on hovering on the like button which is running me into the trouble. Is there any good solution for tackling this scenario?

Share Improve this question edited Jul 21, 2019 at 6:07 Henrik Andersson 47.3k16 gold badges100 silver badges94 bronze badges asked Jul 21, 2019 at 5:57 GigaByteGigaByte 7007 silver badges24 bronze badges 6
  • The DOM element on facebook are dynamically created, when you hover at that time new element is added. – Rishab Commented Jul 21, 2019 at 6:01
  • Yeah exactly, is there any solution or its not possible? – GigaByte Commented Jul 21, 2019 at 6:02
  • Try to trigger mouseenter event on that element, it will create dynamic DOM – Rishab Commented Jul 21, 2019 at 6:05
  • Did you try onmouseover ? – curious_nustian Commented Jul 21, 2019 at 6:08
  • yeah I have tried but it didn't work – GigaByte Commented Jul 21, 2019 at 6:12
 |  Show 1 more ment

2 Answers 2

Reset to default 6

You first need to create an Event and then dispatch that on your required DOM element.

Here is how to create on mouseover event

var event = new MouseEvent('mouseover', {
  'view': window,
  'bubbles': true,
  'cancelable': true
});

Then add an eventListener to your element yourself.

yourelement = document.querySelector('css selector here');
yourelement.addEventListener('mouseover', function() {
      //something here
});

...and then finally dispatchEvent

yourelement.dispatchEvent(event);

I hope it works for whatever you're trying to achieve! Cheers!

Chrome developer tools -> console tab, type:

Locate the element e.g.

 var element = document.querySelectorAll('div.collapse.in.show .milestone-circle')[1]

Add event to the 'element'

 element.dispatchEvent(new MouseEvent('mouseover', {bubbles: true}))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744159400a4561019.html

相关推荐

  • Hover over an element using JavaScript? - Stack Overflow

    Note: I want to hover the element not to trigger an event on hover. (a kind of automated hover using Ja

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信