简体   繁体   中英

Get Element from awesomium webcontrol in c#

I need to get the 'p' element and I use the following code

dynamic document = webControl1.ExecuteJavascriptWithResult("document"); var p = document.getElementsByTagName("p");

but it doesn't work I use Awesomium v1.7.5.1 with visual studio 2010

What specific data do you need from those paragraphs? In the current example that I posted below gets the innerHTML of the each paragraph. The javascript could be:

function GetContents() {
    var arr = [];
    var paragraphs = document.getElementsByTagName('p');
    for (var i = 0; i < paragraphs.length; i++) {
         arr.push(paragraphs[i].innerHTML);
    }
    return arr;
} 
GetContents();

and you can execute this directly by:

JSObject paragraphs = webControl1.ExecuteJavascriptWithResult("function GetContents() {var arr = [];var paragraphs = document.getElementsByTagName('p');for (var i = 0; i < paragraphs.length; i++) {arr.push(paragraphs[i].innerHTML);}return arr;} GetContents();")

In my case however, it seems that I keep on getting an undefined content for the variable paragraphs. The work around for me is putting the GetContents() inside an alert() function.

It becomes this:

JSObject paragraphs = webControl1.ExecuteJavascriptWithResult("function GetContents() {var arr = [];var paragraphs = document.getElementsByTagName('p');for (var i = 0; i < paragraphs.length; i++) {arr.push(paragraphs[i].innerHTML);}return arr;} alert(GetContents());")

and you'll need to wait for the event webControl1.ShowJavascriptDialog to fire.

In Visual Basic.NET, you can do:

Private Sub JSDialog(ByVal sender As Object, ByVal e As JavascriptDialogEventArgs) Handles webControl1.ShowJavascriptDialog
    e.Cancel = False
    e.Handled = True
    MessageBox.Show(e.Message)
End Sub

In C#, you can do it by creating the function:

private void JSDialog(object sender, JavascriptDialogEventArgs e) {
    e.Cancel = false;
    e.Handled = true;
    MessageBox.Show(e.Message);
}

and add this:

webControl1.ShowJavascriptDialog += OnShowJavascriptDialog;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM