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