如何使用纯js单击特定的DOM元素(不是按钮),其行为类似于selenuim.click()

发布于 2025-01-17 19:35:38 字数 1508 浏览 3 评论 0原文

我需要将脚本写入现有站点(我无法更改其代码)。

    orders = document.getElementsByClassName('table-row');
    for (order of orders){
        let text = order.innerText;
        let btn = order.getElementsByClassName('icon-btn')[0];
        btn.click()
    }    

问题是BTN不是按钮。这是一个必须单击的SVG(在网站的内部JS中进行处理),

<svg width="1.2em" height="1.2em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32" class="icon-btn align-middle"><path d="M30 10.68l-2-6A1 1 0 0 0 27 4H5a1 1 0 0 0-1 .68l-2 6A1.19 1.19 0 0 0 2 11v6a1 1 0 0 0 1 1h1v10h2V18h6v10h16V18h1a1 1 0 0 0 1-1v-6a1.19 1.19 0 0 0 0-.32zM26 26H14v-8h12zm2-10h-4v-4h-2v4h-5v-4h-2v4h-5v-4H8v4H4v-4.84L5.72 6h20.56L28 11.16z" fill="currentColor"></path></svg>

因此默认情况下它没有单击()属性

VM844:9 Uncaught TypeError: Cannot read properties of undefined (reading 'click')
    at <anonymous>:9:13

,因此只需单击该元素,然后在该内部网站JS将处理此单击并给我。弹出窗口。

等待的事件就像selenium.click(),只需单击对象,

我尝试添加btn.addeventlistener('click'click',...)

for (order of orders){
        console.log(order)
        let text = order.innerText;
        let btn = order.getElementsByClassName('icon-btn')[0];
        btn.addEventListener("click", function(){ alert("Hello World!"); })
    }   

,我有错误

VM1553:9 Uncaught TypeError: Cannot read properties of undefined (reading 'addEventListener')
    at <anonymous>:9:13
(anonymous) @ VM1553:9

I need to write script to existing site(i can't change its code)

    orders = document.getElementsByClassName('table-row');
    for (order of orders){
        let text = order.innerText;
        let btn = order.getElementsByClassName('icon-btn')[0];
        btn.click()
    }    

The problem is that btn is not button. It's an SVG that has to be clicked on (processed in internal js of website)

<svg width="1.2em" height="1.2em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32" class="icon-btn align-middle"><path d="M30 10.68l-2-6A1 1 0 0 0 27 4H5a1 1 0 0 0-1 .68l-2 6A1.19 1.19 0 0 0 2 11v6a1 1 0 0 0 1 1h1v10h2V18h6v10h16V18h1a1 1 0 0 0 1-1v-6a1.19 1.19 0 0 0 0-.32zM26 26H14v-8h12zm2-10h-4v-4h-2v4h-5v-4h-2v4h-5v-4H8v4H4v-4.84L5.72 6h20.56L28 11.16z" fill="currentColor"></path></svg>

so by default it has not click() property

VM844:9 Uncaught TypeError: Cannot read properties of undefined (reading 'click')
    at <anonymous>:9:13

So the task is just click on that element, and after that internal site js would process this click and give me popup.

Awaited event is like selenium.click() that just immitate click on object

I tried to add btn.addEventListener('click', ...)

for (order of orders){
        console.log(order)
        let text = order.innerText;
        let btn = order.getElementsByClassName('icon-btn')[0];
        btn.addEventListener("click", function(){ alert("Hello World!"); })
    }   

and i got error

VM1553:9 Uncaught TypeError: Cannot read properties of undefined (reading 'addEventListener')
    at <anonymous>:9:13
(anonymous) @ VM1553:9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2025-01-24 19:35:38
document.getElementsByClassName('icon-btn')[0]._vei.onClick.value()
document.getElementsByClassName('icon-btn')[0]._vei.onClick.value()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文