【问题标题】:Click one of contact in contact list in whatsapp web (javascript)单击whatsapp web(javascript)中联系人列表中的联系人之一
【发布时间】:2017-12-12 19:21:07
【问题描述】:

我尝试使用 JavaScript 执行单击操作以单击联系人表单联系人列表,以便在 Whatsapp Web 中打开聊天。

我使用正常的点击动作来点击但不工作,比如

var div = document.querySelector('.infinite-list-item')[0];
div.click();

而且我知道whatsapp web 是由react js 制作的,ant 专门用来执行点击?

我什至使用了 jQuery click() 函数但仍然无法正常工作,我应该使用什么?

【问题讨论】:

  • querySelector() 返回第一个匹配元素...没有querySelector()[0]。除此之外,这个问题太宽泛了。请花点时间阅读How to Askminimal reproducible example
  • 运气好了吗?尝试使用 jQuery 并单击元素及其所有父元素 - 也没有运气。我不确定这是因为开发人员使用了 reactJS 还是因为他们保护了它。
  • 您是否找到了一种解决方案来模拟从浏览器 JS 控制台单击 WhatsApp Web 中的联系人?

标签: javascript jquery html reactjs web


【解决方案1】:

您正在使用document.querySelector(),然后尝试像访问数据结构一样访问它。

document.querySelector() 将只返回一个 DOM 元素。如果您想将所有联系人放在一个结构中,您应该尝试使用document.querySelectorAll()[0]

解决方案:

    var itemList = document.querySelectorAll('.infinite-list-item');
    itemList[0].click();

此外,这也可以作为替代解决方案(尽管完全不推荐使用 querySelector 而不是 querySelectorAll 来定位多次使用的类名):

    var div = document.querySelector('.infinite-list-item');
    div.click();

【讨论】:

  • 感谢您的回答。我尝试了您所有的解决方案,但仍然无法正常工作。我在控制台中尝试了它们,只是返回未定义。聊天未打开。
  • 使用 mousedown 事件。使用鼠标按下打开聊天。函数模拟鼠标事件(元素,事件名称){ var mouseEvent = document.createEvent('MouseEvents'); mouseEvent.initEvent (eventName, true, true); element.dispatchEvent (mouseEvent); } simulationMouseEvents('要点击的元素', "mousedown")
【解决方案2】:
         document.queryselectorAll("div span").forEach((/*YourElement*/, index) =>{
             setTimeout(()=>{
                  
                  /*here should go a validation so that it only runs on the 
                  contacts you want, but i don't write it because that's not the 
                   main idea*/

                 //¡¡¡¡the important part is this!!!!!

                 function simulateMouseEvents (elemento, eventName) {
                     var mouseEvent = document.createEvent ('MouseEvents');
                     mouseEvent.initEvent (eventName, true, true);
                     /*YourElement*/.dispatchEvent (mouseEvent);
                 }
                 simulateMouseEvents ('enlace', "mousedown");

             },index*20)
         }

/这段代码 sn-p 解决了我在个人项目中的问题...我附上项目的图片(未完成,因此可能有错误)以帮助您更好地理解/

Image of the proyect

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 2011-05-16
    • 2017-09-07
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多