我有一个angularjs网页,想要获取指定元素的作用域。但执行reloadWithDebugInfo函数后,结果为空;
private Page _page;
private Browser _browser;
private async void button1_ClickAsync(object sender, EventArgs e)
{
try
{
await initAsync();
await test2Async();
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
private async Task initAsync()
{
_browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false,
ExecutablePath = @"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
Timeout = 60000
});
}
private async Task test2Async()
{
try
{
_page = await _browser.NewPageAsync();
await _page.GoToAsync("https://SOME Angular JS WebPage");
await _page.EvaluateFunctionAsync(@"() => angular.reloadWithDebugInfo()");
var scopeContent = await _page.EvaluateFunctionAsync("() => angular.element(document.getElementsByClassName('left-column-v3')).scope() ");
// scopeContent is null. why? (the above javascript code runs successfully in the chrome dev console.)
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
这些语句在chrome dev工具中工作得很好。我期望作用域的json内容,但这是空的;
更新:对不起,在Scope()之后我忘了一些东西。我需要作用域中的变量,而不是作用域本身:
var scopeContent = await _page.EvaluateFunctionAsync("() => angular.element(document.getElementsByClassName('left-column-v3')).scope().SomeVariable ");
发布于 2019-09-10 19:11:21
问题是作用域函数的结果是不可序列化的。您需要在EvaluateFunctionAsync
中构建一个可序列化的对象并返回该对象。
https://stackoverflow.com/questions/57861475
复制相似问题