reloadWithDebugInfo之后如何获取Angularjs作用域变量?

时间:2019-09-09 21:34:08

标签: puppeteer puppeteer-sharp

我有一个angularjs网页,想获取指定元素的范围。但是在执行reloadWithDebugInfo函数之后,结果为null;

    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开发工具中效果很好。 我希望范围的json内容,但这为null;

更新: 抱歉,在Scope()之后我忘了一些东西。 我想要作用域中的变量,而不是作用域本身:

        var scopeContent = await _page.EvaluateFunctionAsync("() => angular.element(document.getElementsByClassName('left-column-v3')).scope().SomeVariable ");

1 个答案:

答案 0 :(得分:0)

问题是范围函数的结果不可序列化。 您需要在EvaluateFunctionAsync内构建一个可序列化的对象,然后将其返回。

Javascript error