Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way seems to get an error in error case #34

Open
SmolinsCo opened this issue Oct 31, 2018 · 0 comments
Open

No way seems to get an error in error case #34

SmolinsCo opened this issue Oct 31, 2018 · 0 comments

Comments

@SmolinsCo
Copy link

SmolinsCo commented Oct 31, 2018

I have following code based on official sample of this project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Threading;
using Task = System.Threading.Tasks.Task;
using System.Windows.Forms;

using MasterDevs.ChromeDevTools;
using DOM = MasterDevs.ChromeDevTools.Protocol.Chrome.DOM;
using Emulation = MasterDevs.ChromeDevTools.Protocol.Chrome.Emulation;
using Page = MasterDevs.ChromeDevTools.Protocol.Chrome.Page;

namespace FreelanceRuBot
{
    class Program
    {
        const int ViewPortWidth = 1440;
        const int ViewPortHeight = 900;

        static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                var getHTMLDone = new ManualResetEventSlim();

                var chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner());
                using (var chromeProcess = chromeProcessFactory.Create(9222, true))
                {
                    var sessionInfo = (await chromeProcess.GetSessionInfo()).LastOrDefault();
                    var chromeSessionFactory = new ChromeSessionFactory();
                    var chromeSession = chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);

                    await chromeSession.SendAsync(new Emulation.SetVisibleSizeCommand
                    {
                        Width = ViewPortWidth,
                        Height = ViewPortHeight
                    });

                    var navigateResponse = await chromeSession.SendAsync(new Page.NavigateCommand
                    {
                        Url = "http://www.google.com"
                    });

                    var pageEnableResult = await chromeSession.SendAsync<Page.EnableCommand>();

                    chromeSession.Subscribe<Page.LoadEventFiredEvent>(loadEventFired =>
                    {
                        Task.Run(async () =>
                        {
                            var doc = (await chromeSession.SendAsync(new DOM.GetDocumentCommand())).Result.Root;
                                
                            var res = chromeSession.SendAsync(new DOM.GetOuterHTMLCommand());

                            getHTMLDone.Set();
                        });
                    });

                    getHTMLDone.Wait();

                    Console.WriteLine("Exiting ..");
                }
            }).Wait();
        }
    }
}

Some error occurs in this line;

var res = chromeSession.SendAsync(new DOM.GetOuterHTMLCommand());

But it is not issue about an error. It is issue about how to handle such errors.

This line hasn't any result processing, but anyway it crashes with: System.InvalidCastException. Place is ChromeSession.cs, CastTaskResult method, 94 line.
Because it can't cast "MasterDevs.ChromeDevTools.ErrorResponse" to "MasterDevs.ChromeDevTools.CommandResponse`.

I want to obtain ErrorResponse in such case. But it looks like there are no normal way to obtain it.

P. S. I could contribute if you will merge :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant