How to access elements with CEFSharp?

十年热恋 提交于 2019-12-02 08:51:13

问题


I am working with CEFSharp C# for the first time and im having a hard time figuring out how to make the browser do anything but browser.Load(""); I have been searching many websites for hours and hours and nobody seems to have an answer or have this problem. I am trying to access the website elements as if they were c# form controls... in a nutshell. I'm not supposed to ask broad questions... how would I do something like browser.Click("/*elementName*/")? Is Also, is there no way to do something like browser.TextBox1.Text = "blah";?

@Jim W

my code so far: updated 6/6/2018 4:29 pm

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 WebAppWorkAround
{
    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('aaa').value; })();", null);

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





        }
    }
}

errors:

Error CS1061 'Task' does not contain a definition for 'Result' and no extension method 'Result' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?) (line 61)

Error CS0266 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) (line 62)


回答1:


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());





        }


    }
}


来源:https://stackoverflow.com/questions/50688705/how-to-access-elements-with-cefsharp

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