How to execute Javascript function from C # WebBrowser?

I am doing web automation via C # and WebBrowser. There is a link in there that I need 'click', but since it triggers the Javascript function, it is obvious that the code needs to be executed and not just clicked on the element (ie Element.InvokeMember ("click")). Here's the href for the element that opens the Ajax form:

javascript:__doPostBack("ctl00$cphMain$lnkNameserverUpdate", "")

      

I tried:

webBrowser1.Document.InvokeScript("javascript:__doPostBack", new object[] { "ctl00$cphMain$lnkNameserverUpdate", "" });

      

and

webBrowser1.Document.InvokeScript("__doPostBack", new object[] { "ctl00$cphMain$lnkNameserverUpdate", "" });

      

and a few more things. The code hits, but the script doesn't run. Any ideas would be most appreciated.

Gregg

BTW Here's the complete element in case it's useful:

<a href="javascript:__doPostBack('ctl00$cphMain$lnkNameserverUpdate','')" onmouseout="window.status=''; return true" onmouseover="window.status='Update Nameservers'; return true" id="ctl00_cphMain_lnkNameserverUpdate" onclick="javascript:Layout.ChangeIframeToSrc('DropinLoad_Domain.aspx?controlRequest=ActionNameserversWithIP');return false;">NS51.DOMAINCONTROL.COM<br/>NS52.DOMAINCONTROL.COM<br/></a>

      

0


a source to share


2 answers


Take a look at this link:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx



I've actually used this in the past and it works great.

0


a source


HtmlDocument doc = browser.Document;
HtmlElement head = doc.GetElementsByTagName("head")[0];
HtmlElement s = doc.CreateElement("script");
s.SetAttribute("text","function sayhello() { alert('hello'); }");
head.AppendChild(s);
browser.Document.InvokeScript("sayHello");

      



0


a source







All Articles