1

Can you find HTML elements in awesomium webcontrol to do the further processing?

For example, I can find necessary element (or even element collection) in Watin using:

Link playButton = myie.ElementOfType<Link>(Find.ById("myid")); // find link (<a>)
Div test = myie.Div(Find.ById("audio")); // find div (<div>)

When found - you can extract multiple properties of that element

string classname = playButton.ClassName; // alternatively -> inner text, link, id, class and all bucket of other properties

How do I do it in awesomium? Does it have built-in DOM parser to operate with website controls? (divs, links, tables, etc..)

So far I could only find javascript execution command but that's not what I'm looking for..

Additionally, I'd like to know how to save webpage's HTML to string (in awesomium)?

string mysite = webControl1.SiteHTML.ToString(); // something like this
// instead of
string mysite = webControl1.ExecuteJavascriptWithResult("document.documentElement.outerHTML").ToString();

EDIT: explaination

It looks like awesomium doesnt support HTML element parsing natively, so my backup plan is following:

  1. open webpage in awesomium
  2. grab HTML to string (currently using: ExecuteJavascriptWithResult())
  3. parse string to HTML (currently using: "HTML Agility pack")
  4. do what I need to do (find elements, collection, etc.)
  5. execute JS command in awesomium based on previous step results

All of this would be easier if awesomium had DOM support.. but.. well..

Alex
  • 4,607
  • 9
  • 61
  • 99

3 Answers3

6

And how about a JS trick?

public string GetHTML(WebControl control)
{
          return control.ExecuteJavascriptWithResult("document.getElementsByTagName('html')[0].outerHTML");

}

Cheers!

Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63
1

Yes we can found html element to do further processing on. For example;

    dynamic document = (JSObject)webControl.ExecuteJavascriptWithResult("document");

using (document)
                {
                    try
                {
                    dynamic email = document.getElementById("email");
                    dynamic password = document.getElementById("password");

                    if (email == null && password == null)
                        return;

                    using (email)
                    {
                        email.value = "abc@msn.com";
                    }

                    using (password)
                    {
                        password.value = "password";
                    }
                }
                catch (Exception)
                {

                }

            }

It is the way i found the htmlInputElement and i have tested it and it works for me.

nhasan
  • 21
  • 2
  • 7
0

With regards to the "How can I get the HTML" question, I seem to recall using a semi-hacky way of doing this when I ran into this problem:

var control = <some awesomium WebControl instance>;
control.SelectAll();
control.CopyHtml();
var html = Clipboard.GetText();
JerKimball
  • 16,584
  • 3
  • 43
  • 55
  • well, I hoped that there is a more native way of copying HTML.. Copy from clipboard is certainly not what I'd call native (and clever) ;) – Alex Feb 07 '13 at 21:45
  • that's strange but your approach is not working.. nothing is copied to clipboard. P.S. yes, i've changed CopyHtml to CopyHTML. Seems like selectall is not selecting anything – Alex Feb 07 '13 at 22:03
  • That's strange...it's been a while since I had to muck with this, maybe I'm missing a step. When I get back in front of my computer I'll rig up a quick test harness. – JerKimball Feb 07 '13 at 22:08
  • ok. In my case - SelectAll() doesn't select anything. That's confirmed. However, if I do the click on webcontrol followed by ctrl+A, then your approach works fine. (HTML is copied to clipboard) – Alex Feb 07 '13 at 22:18
  • Oh! Isn't there a `FocusView` or similar on the control? – JerKimball Feb 07 '13 at 22:21
  • after restart - selectall works but copyhtml isn't.. ok, I give up. I'll mark your answer as a correct one just in case. awesomium is so useless for me – Alex Feb 08 '13 at 08:43