如何使用CEFSharp访问元素? [英] How to access elements with CEFSharp?

查看:203
本文介绍了如何使用CEFSharp访问元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用CEFSharp C#,并且我很难解决如何使浏览器执行除browser.Load();之外的任何事情。
我一直在搜索许多网站,时间又小时,似乎没人能找到答案或有这个问题。我试图访问网站元素,就像它们是c#表单控件一样。我不应该问广泛的问题...我该怎么做 browser.Click( / * elementName * /)这样的事情?另外,还有没有办法像 browser.TextBox1.Text = blah; 吗?

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

我到目前为止的代码:更新2018年6月6日4:29 pm

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





        }
    }
}

错误:

错误CS1061'任务'不包含'结果'的定义,也没有扩展方法'会发现结果接受类型为任务的第一个参数(您是否缺少using指令或程序集引用?)(第61行)

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)

错误CS0266无法隐式将类型对象转换为字符串。存在显式转换(您是否缺少强制转换?)(第62行)

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

推荐答案

它看起来并不像就像调用

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

browser.TextBox1.Text = "blah"; 

据我所知,CEFSharp是一种从C#包装程序执行Javascript的方法。它不是DOM的C#版本(这就是它所需要的)。

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).

因此,从Github Wiki,我会说您需要使用此代码

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

然后 EvaluateJavaScriptResult 中应包含

这是一个完整的示例,设计人员只需要一个Panel(panel1)和一个Button(button1):

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

运行它,在搜索框中输入内容(在Wikipedia中),然后单击按钮,您应该会看到一个带有搜索框内容的消息框

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





        }


    }
}

这篇关于如何使用CEFSharp访问元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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