无法在“ Web应用程序”中使用javascript模拟点击事件

问题描述 投票:0回答:2

我已经搜索了很多有关此的信息,但尚未找到解决方案。

我的问题如下,我正在创建扩展程序以加快浏览多个网页的速度,我已经用许多网页进行了管理,但是我遇到了一些我无法使用Javascript模拟点击的问题,但是我没有不知道该怎么做。

其中一个页面是:https://sports.betway.es/es/sports/in-play该页面是西班牙语域,因此,尽管我认为使用域“ .com”,该页面仍然有效,但我不知道他们是否可以从另一个国家(不使用vpn)访问该页面。

代码如下,非常简单。

var deportesActivos = document.getElementsByClassName("categoryListItemWrapper contentSelectorItemButton")
for(let i=0;deportesActivos.length;i++){
    let nombre = deportesActivos[i].lastChild.firstChild.innerText
    if(nombre == data.deporte){
        deportesActivos[i].click()
    }
}

deportesActivos我从页面中收集具有该类的DIV元素。deportesActivos[i].lastChild.firstChild.innerText我提取了每个元素的文本

if(nombre == data.deporte){
  deportesActivos[i].click()
}

匹配时,单击以进入链接。

问题是点击无法模拟我。我已经完全购买了您单击的元素,我手动单击了该元素,并且它起作用了,我试图单击该网页上的其他元素,但是它不起作用,我试图给该对象一个“单击时听”元素,它也不起作用。页面的HTML如下:Image with HTML Code of the website

javascript html web-applications click
2个回答
0
投票

我认为问题出在这一行:

for(let i=0;deportesActivos.length;i++)

因为您的停止条件是deportesActivos.length,所以每当deportesActivos.length是真实值时,都会进行无限循环。它应该与类似的东西一起工作:

for(let i=0; i < deportesActivos.length;i++)

0
投票

click事件未完全模拟用户单击。它只是在您要定位的元素(和任何父元素)上触发onClick处理函数。

[如果您只是在单击按钮时重定向到新的URL,则可以在循环中执行此操作。

// get the links, not the buttons

var deportesActivos = document.getElementsByClassName("categoryListItemWrapper contentSelector");

for (let i=0; i < deportesActivos.length; i++) {
    // Drill down one extralevel to get the button text
    let nombre = deportesActivos[i].firstChild.lastChild.firstChild.innerText;
    if (nombre === data.deporte) {
        // Redirect to the href in the link        
        window.location.href = deportesActivos[i].href;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.