为什么element.click()在Cefsharp中不起作用? [英] Why element.click() not working in Cefsharp?

查看:449
本文介绍了为什么element.click()在Cefsharp中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Cefsharp 中执行以下脚本以单击DIV元素,但不起作用。

I try to execute below script in Cefsharp to click on a DIV element, and not working.

private static string ClickUnreads = @"(function() {
                                        let chatsEl = document.querySelectorAll('.infinite-list-item');
                                        for (let x = 0; x < chatsEl.length; x++) {
                                            let unread = chatsEl[x].getElementsByClassName('unread');
                                            if (unread.length > 0) {
                                                chatsEl[x].click();
                                            }
                                        }
                                    })();";

public void ClickUnreads()
{
    WebBrowser.ExecuteScriptAsync(ClickUnreads);
}

我在这里发现了与我类似的类似问题,但是没有一个

I found similar threads here that have problem similar to mine, but none of it's answer working.

我想使用基于 Selenium 的解决方案,就像在Java中一样,但是我需要将浏览器嵌入到我的应用程序中,而且我不能使用无头浏览器,因为我要自动化的站点需要QR码身份验证。

I wanna use Selenium based solution, like I did in Java, but I need the browser to be embedded inside my app, and I can't use Headless browser, because the site which I'm trying to automate requires QR code authentication.

是否可以使用 Chromium Web驱动程序来控制 Cefsharp ?如果可能的话,有人可以帮我照亮吗? (显示链接)。

Is it possible to use Chromium web driver to control Cefsharp? If possible, anybody can shed a light for me? (show me the link of it).

下面是我在Java中使用Chrome网络驱动程序编写的代码,它的工作原理类似于charm

Below is my code in Java, using Chrome web driver, and it works like charm

while (true) {
    List<WebElement> chatItems = chatsList.findElements(By.className("infinite-list-item"));
    for (WebElement el : chatItems) {
        WebElement unread = el.findElement(By.className("unread"));
        if (unread != null) {
            unread.click();
        }
    }
    Thread.sleep(10000);
}

我选中了此链接也是,以查找是否存在另一个可嵌入的浏览器,但是我没有时间测试它们(建议

I checked this link too, to find out if there is another embeddable browser, but I haven't got time to test them all (any recommendation would be appreciated).

谢谢

推荐答案

好的,

我通过此线程
所以我将js脚本更改为此

I managed to solved this, from the help of this thread. So I changed my js script to this

@"(function(x) {
let chatsEl = document.querySelectorAll('.infinite-list-item');
for (let x = 0; x < chatsEl.length; x++) {
    let unread = chatsEl[x].getElementsByClassName('unread');
    if (unread.length > 0) {
        let hoverEvent = document.createEvent ('MouseEvents');
        hoverEvent.initEvent ('mouseover', true, true);
        unread[0].dispatchEvent (hoverEvent);

        let downEvent = document.createEvent ('MouseEvents');
        downEvent.initEvent ('mousedown', true, true);
        unread[0].dispatchEvent (downEvent);

        let clickEvent = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        unread[0].dispatchEvent (clickEvent);
    }
}
})();";

然后我设法单击了未读邮件。

And I managed to click on the unread messages.

这篇关于为什么element.click()在Cefsharp中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆