How to access elements with CEFSharp?

只谈情不闲聊 提交于 2019-12-02 04:24:34

It doesn't look like it's going to be as straightforward as calling

browser.TextBox1.Text = "blah"; 

As I understand it, CEFSharp is a way to execute Javascript from the C# wrapper. It's not a C# version of the DOM (which is what it'd need to be).

So, from the Github wiki I would say you need to use this code

string EvaluateJavaScriptResult;
var frame = chromeBrowser.GetMainFrame();
var task = frame.EvaluateScriptAsync("(function() { return document.getElementById('<textBoxElementID>').value; })();", null);

task.ContinueWith(t =>
{
    if (!t.IsFaulted)
    {
        var response = t.Result;
        EvaluateJavaScriptResult = response.Success ? (response.Result.ToString() ?? "null") : response.Message;
    }
}, TaskScheduler.FromCurrentSynchronizationContext());

and then EvaluateJavaScriptResult should have the text in the textbox.

Here's a complete working example, the designer just needs a Panel (panel1) and a Button (button1):

Run it, type something in the search box (in Wikipedia), and then hit the button, you should see a messagebox with the content of the search box

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeChromium();
        }
        List<string> classList = new List<string>();
        public ChromiumWebBrowser chromeBrowser;

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            Cef.Initialize(settings);
            chromeBrowser = new ChromiumWebBrowser("https://en.wikipedia.org/wiki/Main_Page");
            this.panel1.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }
        public string extract;

        private void button1_Click(object sender, EventArgs e)
        {

            string EvaluateJavaScriptResult;
            var frame = chromeBrowser.GetMainFrame();
            var task = frame.EvaluateScriptAsync("(function() { return document.getElementById('searchInput').value; })();", null);

            task.ContinueWith(t =>
            {
                if (!t.IsFaulted)
                {
                    var response = t.Result;
                    EvaluateJavaScriptResult = response.Success ? (response.Result.ToString() ?? "null") : response.Message;
                    MessageBox.Show(EvaluateJavaScriptResult);
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());





        }


    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!