C# CefSharp chromeBrowser 尝试模拟按键和鼠标点击

问题描述 投票:0回答:1

在查看相关帖子和文档后我感到很茫然,许多其他人似乎也遇到了类似的问题,但现有的解决方案都没有导致浏览器真正识别任何按键。

我的所有代码都在下面,只是一个空表单,试图在当前没有 API 但即将推出的网站上输入用户名/密码。所以这只是一个创可贴修复,直到他们的 API 变得充实为止。大多数情况下只需要登录并向网站发送按键,但似乎没有任何作用。

我也尝试过:https://ourcodeworld.com/articles/read/520/simulated-keypress-in-the-right-way-using-inputsimulator-with-csharp-in-winforms

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;


namespace Hydros_tmp_api
{
    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;
        string CurrentAddress = "";

        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("https://www.coralvuehydros.com/app/#/status");
            // Add it to the form and fill it to the form window.
            Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
            (chromeBrowser).LoadingStateChanged += Browser_FrameLoadEnd;

            chromeBrowser.AddressChanged += Browser_AddressChanged;
        }
        public Form1()
        {
            InitializeComponent();
            InitializeChromium();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        async void Browser_FrameLoadEnd(object sender, LoadingStateChangedEventArgs e)
        { // Jumps in here when the page is fully loaded:
            if (e.IsLoading == false)
            {
                if (CurrentAddress.Contains("login"))
                {
                    timer_login.Enabled = true;
                }
            }
        }

        private void Browser_AddressChanged(object sender, AddressChangedEventArgs e)
        {
            CurrentAddress = e.Address;
        }

        private void timer_login_Tick(object sender, EventArgs e)
        {
            timer_login.Enabled = false;
                    chromeBrowser.GetBrowserHost().SetFocus(true);
                    Random rnd = new Random();


                    chromeBrowser.GetBrowser().GetHost().SendKeyEvent(new KeyEvent
                    {
                        WindowsKeyCode = (int)Keys.Tab, // Enter
                        FocusOnEditableField = true,
                        IsSystemKey = false,
                        Type = KeyEventType.KeyDown
                    });

                    System.Threading.Thread.Sleep(rnd.Next(20, 120));

                    chromeBrowser.GetBrowser().GetHost().SendKeyEvent(new KeyEvent
                    {
                        WindowsKeyCode = (int)Keys.Tab, // Enter
                        FocusOnEditableField = true,
                        IsSystemKey = false,
                        Type = KeyEventType.KeyUp
                    });

                    System.Threading.Thread.Sleep(rnd.Next(20, 120));

                    chromeBrowser.GetBrowser().GetHost().SendKeyEvent(new KeyEvent
                    {
                        WindowsKeyCode = (int)Keys.T, // Enter
                        FocusOnEditableField = true,
                        IsSystemKey = false,
                        Type = KeyEventType.KeyDown
                    });

                    System.Threading.Thread.Sleep(rnd.Next(20, 120));

                    chromeBrowser.GetBrowser().GetHost().SendKeyEvent(new KeyEvent
                    {
                        WindowsKeyCode = (int)Keys.T, // Enter
                        FocusOnEditableField = true,
                        IsSystemKey = false,
                        Type = KeyEventType.KeyUp
                    });

                    System.Threading.Thread.Sleep(rnd.Next(20, 120));

        }

    }
}

编辑:他们的网站似乎有一个特殊的奇怪之处,因为它在其他网站上运行良好,所以不接受模拟输入?所以我不知道从这里去哪里。

c# cefsharp
1个回答
0
投票

抱歉带了这个备份,但我有同样的问题,你找到解决方案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.