SuperDave
SuperDave

Reputation: 413

CefSharp and frames, only retrieving HTML from first frame

pcpao.org/general.php?strap=152814186280001650

In trying to get the full HTML from that site, .GetSourceAsync and .ViewSource, both only show the 'frameset' HTML. Using the ShowDevTools option, the full HTML data is in both the elements collection and the Sources of the Chrome-devtools.

I am running this after the webpage has loaded, but it should all be there still since its in the dev-tools?

What am I missing to get the full HTML out of a navigated site. I suspect this has to do with frames but after an hour of googling and reading old messages I see this only tangentially mentioned.

Winforms

  package id="cef.redist.x64" version="3.2526.1362" targetFramework="net46"
  package id="cef.redist.x86" version="3.2526.1362" targetFramework="net46"
  package id="CefSharp.Common" version="47.0.3" targetFramework="net46"
  package id="CefSharp.WinForms" version="47.0.3" targetFramework="net46"

Upvotes: 4

Views: 9841

Answers (2)

SuperDave
SuperDave

Reputation: 413

Thanks, some examples from previous versions had me confused how this works. I was looking for something like this.

var frameIdent = Browser.GetBrowser().GetFrameIdentifiers();
var result = Browser.GetBrowser().GetFrame(frameIdent.Last()).GetSourceAsync().Result;
textBox1.Text = result.ToString();

So I guess the way to get all HTML from a site is loop through the frames identifiers list, get the result from each frame via GetSourceAsync and concatenate them all to a string.

Upvotes: 4

Jim Wilcox
Jim Wilcox

Reputation: 1583

I was having the same issue trying to get click on and item located in a frame and not on the main frame. Using the example in your answer, I wrote the following extension method:

public static IFrame GetFrame(this ChromiumWebBrowser browser, string FrameName)
{
    IFrame frame = null;

    var identifiers = browser.GetBrowser().GetFrameIdentifiers();

    foreach (var i in identifiers)
    {
        frame = browser.GetBrowser().GetFrame(i);
        if (frame.Name == FrameName)
            return frame;
    }

    return null;
}

If you have a "using" on your form for the module that contains this method you can do something like:

var frame = browser.GetFrame("nameofframe");
if (frame != null)
    frame.EvaluateScriptAsync("document.getElementById('whateveridyouwanttoclick').click();");

Of course you need to make sure the page load is complete before using this, but I plan to use it a lot. Hope it helps!

Upvotes: 10

Related Questions