CEFSHARP - ChromiumWebBrowser 获取 table 行单元格值
CEFSHARP - ChromiumWebBrowser on get table row cell values
我解决了大部分问题,但找不到其中一个问题的解决方案。
以前,我使用 Windows WebBrowser 并在经销商面板的查询屏幕上获得 table 结果。像这样;
foreach (HtmlElement htmlobj in webBrowser1.Document.GetElementsByTagName("Table"))
{
if (htmlobj.GetAttribute("className") == "table table-striped")
{
foreach (HtmlElement tr in htmlobj.GetElementsByTagName("tbody"))
{
foreach (HtmlElement td in tr.Children)
{
string s0 = td.Children[0].InnerText;
string s1 = td.Children[1].InnerText;
}
}
}
}
如何使用 ChromiumWebBrowser 执行此操作?
非常感谢您的帮助...^_^
这可以在 CefSharp 中使用 EvaluateScriptAsync
实现,方法是执行 JavaScript 找到 table 的 td
元素并将它们 return 作为数组,C# 将接收为 List<object>
如果您不知道,您通常必须等到页面的 MainFrame
加载后才能执行任何 JavaScript,您可以像这样实现:
//...
ChromiumWebBrowser browser = new ChromiumWebBrowser("https://example.com"); // <- Your URL there
browser.FrameLoadEnd += (sender, args) =>
{
// Wait for the MainFrame to finish loading
if(args.Frame.IsMain)
GetTable(); // method to call that executes JavaScript, etc
}
More information about when you can start executing JavaScript.
现在,这是我为 EvaluateScriptAsync
到 return 和 td
的 innerText
编写的 C# 代码:
// assuming "browser" is the ChromiumWebBrowser and you're ready to start executing JavaScript (see above code)
private void GetTable ()
{
const string script = @"(function(){
let table = document.querySelector('table.table.table-striped'); // <table class='table table-striped'>
let td = table.getElementsByTagName('td');
return [ td[0].innerText, td[1].innerText ];
})();";
browser.GetMainFrame().EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;
if (response.Success && response.Result != null)
{
// We cast values as CefSharp wouldn't know what to expect
List<object> jsResult = (List<object>)response.Result;
string s1 = (string)jsResult[0]; // td[0].innerText
string s2 = (string)jsResult[1]; // td[1].innerText
Console.WriteLine("s1: " + s1);
Console.WriteLine("s2: " + s2);
// In my example HTML page, it will output:
// s1: This is 1st
// s2: This is 2nd
}
});
}
我做了这个 HTML 例子来测试它,因为你没有提供:
<table class="table table-striped">
<tr>
<th>One</th>
<th>Two</th>
</tr>
<tr>
<td>This is 1st</td>
<td>This is 2nd</td>
</tr>
</table>
我解决了大部分问题,但找不到其中一个问题的解决方案。 以前,我使用 Windows WebBrowser 并在经销商面板的查询屏幕上获得 table 结果。像这样;
foreach (HtmlElement htmlobj in webBrowser1.Document.GetElementsByTagName("Table"))
{
if (htmlobj.GetAttribute("className") == "table table-striped")
{
foreach (HtmlElement tr in htmlobj.GetElementsByTagName("tbody"))
{
foreach (HtmlElement td in tr.Children)
{
string s0 = td.Children[0].InnerText;
string s1 = td.Children[1].InnerText;
}
}
}
}
如何使用 ChromiumWebBrowser 执行此操作?
非常感谢您的帮助...^_^
这可以在 CefSharp 中使用 EvaluateScriptAsync
实现,方法是执行 JavaScript 找到 table 的 td
元素并将它们 return 作为数组,C# 将接收为 List<object>
如果您不知道,您通常必须等到页面的 MainFrame
加载后才能执行任何 JavaScript,您可以像这样实现:
//...
ChromiumWebBrowser browser = new ChromiumWebBrowser("https://example.com"); // <- Your URL there
browser.FrameLoadEnd += (sender, args) =>
{
// Wait for the MainFrame to finish loading
if(args.Frame.IsMain)
GetTable(); // method to call that executes JavaScript, etc
}
More information about when you can start executing JavaScript.
现在,这是我为 EvaluateScriptAsync
到 return 和 td
的 innerText
编写的 C# 代码:
// assuming "browser" is the ChromiumWebBrowser and you're ready to start executing JavaScript (see above code)
private void GetTable ()
{
const string script = @"(function(){
let table = document.querySelector('table.table.table-striped'); // <table class='table table-striped'>
let td = table.getElementsByTagName('td');
return [ td[0].innerText, td[1].innerText ];
})();";
browser.GetMainFrame().EvaluateScriptAsync(script).ContinueWith(x =>
{
var response = x.Result;
if (response.Success && response.Result != null)
{
// We cast values as CefSharp wouldn't know what to expect
List<object> jsResult = (List<object>)response.Result;
string s1 = (string)jsResult[0]; // td[0].innerText
string s2 = (string)jsResult[1]; // td[1].innerText
Console.WriteLine("s1: " + s1);
Console.WriteLine("s2: " + s2);
// In my example HTML page, it will output:
// s1: This is 1st
// s2: This is 2nd
}
});
}
我做了这个 HTML 例子来测试它,因为你没有提供:
<table class="table table-striped">
<tr>
<th>One</th>
<th>Two</th>
</tr>
<tr>
<td>This is 1st</td>
<td>This is 2nd</td>
</tr>
</table>