admin管理员组文章数量:1422323
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("");
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());
}
}
}
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/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());
}
}
}
Share
Improve this question
edited Jun 18, 2018 at 18:00
SJ10
asked Jun 14, 2018 at 18:42
SJ10SJ10
3351 gold badge5 silver badges13 bronze badges
3
- You need to show the code that doesn't work, even though you've explained what change you made, we need to see it as it is in your file. – Jim W Commented Jun 14, 2018 at 21:04
- I was about to add this to the question when I realized that after replicating the code I get a different error. I'm at a loss for an explanation, because i copy and pasted the same code from before. Ill get back to you on this when possible. – SJ10 Commented Jun 15, 2018 at 21:10
- @Jim W I have updated the question. – SJ10 Commented Jun 18, 2018 at 18:00
1 Answer
Reset to default 5What 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.
本文标签: javascriptC Cefsharp unable to getElementsByClassNameStack Overflow
版权声明:本文标题:javascript - C# Cefsharp unable to getElementsByClassName - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745363176a2655388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论