问题
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.
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.
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).
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).
Thanks
回答1:
Okay,
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.
来源:https://stackoverflow.com/questions/45844533/why-element-click-not-working-in-cefsharp