尝试使用JavaScript在CefSharp3(OffScreen)中模拟用户输入 [英] Trying to simulate user inputs in CefSharp3 (OffScreen) using JavaScript

查看:2269
本文介绍了尝试使用JavaScript在CefSharp3(OffScreen)中模拟用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript模拟CefSharp(OffScreen)上的用户操作。

I am trying to simulate the user operation on CefSharp(OffScreen) using JavaScript.

加载页面后( https://www.w3 .org ),

我想在搜索栏中搜索,

点击搜索按钮

打开第一个结果

所以我用过,

            await browser.EvaluateScriptAsync("document.getElementsByName('q')[0].value = 'CSS';");
            await browser.EvaluateScriptAsync("document.getElementById('search-submit').click();");
            await browser.EvaluateScriptAsync("document.getElementById('r1-0').click();");

但我面临的问题是,截屏时,我必须使用线程。在执行下一操作或截取屏幕截图之前,在页面之间和结尾处睡眠(x)

But the issue I am facing is, to take screenshot, I have to use Thread.Sleep(x) in between and at the end for the pages to load before doing the next operation or take the screenshot.

无论如何都要避免 SLEEP 并检测加载完成后,进行下一步操作?

Is there anyway to avoid the SLEEP and detect when the loading is done, to do the next operation?

我也尝试了 ExecuteScriptAsync ,同样的问题。

I tried ExecuteScriptAsync also, same issue with that also.

推荐答案

您可以为此目的修改和使用 OffScreenExample 中的 LoadPageAsync()功能。

只需替换alter the你不会使用它时删除 url 的参数,并使用 url 删除if语句。

然后调用 LoadPageAsync EvaluateScriptAsync 与相应的浏览器对象之后。

You can modify and use the LoadPageAsync() function from the OffScreenExample for this purpose.
Just replace alter the parameters by removing the url as you wont be using it, and remove the if statement using the url.
Then on calling the LoadPageAsync after EvaluateScriptAsync with the corresponding browser object.

修改后,函数将如下所示。

The Function will look like this after modification.

public static Task LoadPageAsync(IWebBrowser browser)
{
    var tcs = new TaskCompletionSource<bool>();

    EventHandler<LoadingStateChangedEventArgs> handler = null;
    handler = (sender, args) =>
    {
        if (!args.IsLoading)
        {
            browser.LoadingStateChanged -= handler;
            tcs.TrySetResultAsync(true);
        }
    };

    browser.LoadingStateChanged += handler;
    return tcs.Task;
}

用法如下,

await browser.EvaluateScriptAsync("document.getElementsByName('q')[0].value = 'CSS';");
await browser.EvaluateScriptAsync("document.getElementById('search-submit').click();");
await LoadPageAsyncCompleted(browser);
await browser.EvaluateScriptAsync("document.getElementById('r1-0').click();");
await LoadPageAsyncCompleted(browser);

这篇关于尝试使用JavaScript在CefSharp3(OffScreen)中模拟用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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