I am trying to read text from elements by class name in cefsharp. This code from @Jim W does a similar task, only it uses ID instead of class
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());
}
Upon changing string EvaluateJavaScriptResult;
to obj EvaluateJavaScriptResult;
and document.getElementById
to document.getElementsByClassName
and providing the class name as well as the index, I get error: "Uncaught SyntaxError: missing ) after argument list @:1:53"
as the return result. I cannot find where the missing )
is and I have tried lots of different placements of (
and )
in many places and I don't think this is the real error anymore. Everything I have tried has still given me the error @:1:53 (the location?) and I think that means its erroring before it attempts to look for the class name. Does anyone know how I can successfully make cefsharp return the text value of something found by classname?
Edit: upon running a copy and pasted selection of the code I am referencing here, I actually get a different problem, now it just returns null. (This really should just be the same code)
this is the code that returns null
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)
{
object EvaluateJavaScriptResult;
var frame = chromeBrowser.GetMainFrame();
var task = frame.EvaluateScriptAsync("(function() { return document.getElementsByClassName('mw-headline')[0].value; })();", null);
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
MessageBox.Show(EvaluateJavaScriptResult.ToString());
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
What element is mw-headline
, does that have a .value
property? Probably not, given what's happening.
Assuming that mw-headline
is an element like DIV
or SPAN
, you can use
var task = frame.EvaluateScriptAsync("(function() { return document.getElementsByClassName('mw-headline')[0].innerText; })();", null);
This works because value
is for text boxes and input fields, whereas static markup elements like DIV
have innerText and innerHTML.