2

I have been trying to do this for ages, even looked at:

C# WebBrowser control -- Get Document Elements After AJAX?

With no luck.

Basically, I need to interact with elements that are generated at runtime using javascript on the webpage.

As you all know, when you generate an element at runtime using javascript, it doesn't show in the "view source", and thus it does not show when I request WebBrowser.DocumentText.

I am sure there is a way to do this, but there is no information anywhere about this.

Can someone break the ice and tell me how to do it?

Thanks

Community
  • 1
  • 1
TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101

1 Answers1

1

If you also control the web page that you are viewing within the WebBrowser control, then you could expose JavaScript methods that return what you need and use Document.InvokeScript to get the value from the JavaScript method.

C#:

object value = this.WebBrowser1.Document.InvokeScript("getValue");

JavaScript:

function getValue() {
    var editor = iged_getById("<%=WebHtmlEditor1.ClientID %>");
    return editor.getText();
}

If you don't have control over the web site, then you could interact with the document of the webpage through the Document property as that is a .NET object representing the HtmlDocument: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.aspx

alhalama
  • 3,218
  • 1
  • 15
  • 21
  • I have moved away from the WebBrowser control because it is very buggy and hacky when one requires to interact with the webpage extensively. I now use raw requests. However, I think you can get post javascript reference by using the DomDocument instead of Document. – TheGateKeeper May 04 '12 at 23:03