使用C#中的计时器刷新网站-CefSharp

时间:2019-05-13 12:44:18

标签: c# cefsharp

我想为该网站创建一个机器人,该站点添加一个带有 id 的部件,我希望当该部件添加到站点时单击此部件按钮。 我的代码检查了一个网站以找到此 id ,直到未找到该 id 为止,该网站正在刷新,并且当找到 id 时,单击<网站上的em>按钮。

这是我的代码:

public ChromiumWebBrowser browser;

public void InitBrowser()
{
    timer.Stop();
    browser.Reload();

    browser.FrameLoadEnd += (sender, args) =>
    {
        //Wait for the MainFrame to finish loading
        if (args.Frame.IsMain)
        {
            var pageQueryScript = @"
            (function(){
                var selection = document.querySelector('#test');
                if (selection !== null) {
                    return '1';
                }
            })()";
            var scriptTask = browser.EvaluateScriptAsync(pageQueryScript);
            scriptTask.ContinueWith(a =>
            {
                if (a.Result.Success && a.Result.Result != null)
                {
                    timer.Stop();
                    var loginScript = @"document.querySelector('button').click();";
                    browser.EvaluateScriptAsync(loginScript).ContinueWith(b =>
                    {
                        timer.Stop();
                        Console.Write("*");
                    });
                }
                else
                {
                    timer.Start();
                }
            });
        }
    };
    timer.Start();
}

public Form1()
{
    Cef.Initialize(new CefSettings());
    browser = new ChromiumWebBrowser("file:///G:/2.html");
    this.Controls.Add(browser);
    browser.Dock = DockStyle.Fill;
    browser.Height = 500;
    browser.Width = 500;
    browser.Location = new Point(0, 0);

    InitializeComponent();
}

private void timer_Tick(object sender, EventArgs e)
{
    InitBrowser();

}

private void Form1_Load(object sender, EventArgs e)
{
    timer.Start();
}

它可以正常工作,但是它单击了网站刷新的按钮编号,我想单击此按钮一次。 我该如何解决这个问题。

0 个答案:

没有答案
相关问题