I am using WPF WebBrowser control and I want to acces some of the JavaScript functions but there is the problem.
I can use InvokeScript and execute browser.InvokeScript("alert", "Hello");q but how to get element by ID or by TAG and how to assign that element to javascript var?
Example:
Javascript:
var elements = document.getElementsByTagName("embed");
elements[0].doSomething();
C#:
How?
I tryed everything but nothing worked. Can anyone help me :(
Quite a late answer, but if anyone else needs it:
The direct C#: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("embed");
foreach (HtmlElement elem in elems)
{
elem.InvokeMember("doSomething");
}
The alternative: http://msdn.microsoft.com/en-us/library/a0746166
Basically you should create a function in JS:
var myCustomFunc = function(tagName) {
var elements = document.getElementsByTagName(tagName); elements[0].doSomething();
}
And then call it from C# with
webBrowser1.Document.InvokeScript("myCustomFunc ", new String[] { "embed" });
The variable "tagName" gets replaced with "embed"
Related
i want to get element by id and then get attribute and etc....
in web browser i use from this code :
HtmlElement element = wb.Document.Body.Document.GetElementById("dnn_ctr730_ViewTMUrbanFileStatusFromWebService_fb_Captcha_CaptchaImageUP");
if (element != null)
{
string link = element.GetAttribute("src");
but in awesomium how can i do this ?
and also when i want to set value to element in web browser using from this code :
wb.Document.GetElementById("txtFileNo").SetAttribute("Value", "12345");
wb.Document.GetElementById("BTN").InvokeMember("click");
but i don't know how can i do this in awesomium ....
i found this code for set value :
dynamic document = (JSObject)webctrl.ExecuteJavascriptWithResult("document");
if (document == null)
return "";
using (document)
{
dynamic elem = document.getElementById("txt1");
if (elem == null)
return "";
using (elem)
elem.value = "test";
but i don't know how to invoke Click and also how to get attribute value...
anye one can help me ..?
Kind regards
I would use jQuery's attr() and jQuery's trigger():
webctrl.ExecuteJavascript("$(#txtFileNo).attr('value', '12345');");
webctrl.ExecuteJavascript("$(#BTN).trigger('click');");
Since you are targeting a single browser, you could use plain old Javascript for this against the Chromium DOM. But, I find jQuery's trigger() to be much easier to use than the alternative.
Normally I can access html tags and I can set their values via the code something like;
HtmlElementCollection coll = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement curElement in coll)
{
if (curElement.GetAttribute("name").ToString() == "login")
{
curElement.SetAttribute("Value", "123456789");
}
}
But if html input area exist in a iframe tag, This code didnt work,
And I changed line:
var coll = webBrowser1.Document.GetElementsByTagName("input");
to
var coll = webBrowser1.Document.Window.Frames[0].Document.GetElementsByTagName("iframe")
But it still didnt work. Please Help. I cannot get over this problem since last week
Your code looks like it should work if you change this line:
coll = webBrowser1.Document.Window.Frames[0].Document.GetElementsByTagName("iframe")
to
coll = webBrowser1.Document.Window.Frames[0].Document.GetElementsByTagName("input");
Here is a working example...
HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
HtmlElement input = iframe.Document.GetElementsByTagName("input")[0];
input.SetAttribute("value", "Test");
This assumes obviously that you only have at least one iframe element and at least one input element in the child document.
I am trying to build a dropdown as below.
I am generating html ul & li tags as below and works fine. However, everytime there is an anch tag (see the in-line comment); I need to be able to call a function and pass a text that was clicked. How can I do this?
foreach (var productType in productTypesNames.Keys)
{
var li = new HtmlGenericControl("li");
nav.Controls.Add(li);
var ul = new HtmlGenericControl("ul");
var anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
foreach (var pName in productTypesNames[productType] )
{
var subLi = new HtmlGenericControl("li");
var anch = new HtmlGenericControl("a");
anch.Attributes.Add("href", "#");
//**THIS NEEDS TO CALL A C# FUNCTION AND PASS pName; instead of #**
anch.InnerHtml = pName;
subLi.Controls.Add(anch);
ul.Controls.Add(subLi);
}
anchor.InnerHtml = productType;
li.Controls.Add(anchor);
li.Controls.Add(ul);
}
If you don't mind a postback, or want it, use a LinkButton control, easily built dynamically instead of your HtmlGenericControl("a"), and have the server-side onclick method call your other method.
Otherwise you need AJAX as others have explained.
Have your href execute a JS function and that can call the server via AJAX. You can had code your own, or use jQuery or [WebMethod]s with ASP.NET Ajax.
The idea is that you can execute a server-side method by calling the server via Ajax.
Also, look at the way JSONP works - maybe you can "ping" another page with URL parameters of your choosing to do a one-way async call.
I use C#.net.
I wrote JavaScript for hide and show expand and collapse div accordingly. It work well in IE but not on Firefox, not even call the JavaScript function and gives me error as Error: ctl00_cpContents_dlSearchList_ctl08_profiledetailscollapse is not defined.
My JavaScript is as follows
function displayDiv(divCompact, divExpand) {
//alert('1');
var str = "ctl00_cpContents_";
var divstyle = new String();
// alert("ibtnShowHide" + ibtnShowHide);
divstyle = divCompact.style.display;
if (divstyle.toLowerCase() == "block" || divstyle == "") {
divCompact.style.display = "none";
divExpand.style.display = "block";
// ibtnShowHide.ImageUrl = "images/expand_img.GIF";
}
else {
// ibtnShowHide.ImageUrl = "images/restore_img.GIF";
divCompact.style.display = "block";
divExpand.style.display = "none";
}
return false;
}
ctl00_cpContents_dlSearchList_ctl08_profiledetailscollapse is an element id generated by ASP.NET. It's a profiledetailscollapse control inside dlSearchList.
JavaScript variable "ctl00_cpContents_dlSearchList_ctl08_profiledetailscollapse" is not
defined. Firefox does not automatically create, for each element with an id, a
variable in the global scope named after that id and containing a reference
to the element.
You might want to consider using jQuery to make sure that your DOM manipulation is cross-browser compatible.
I'm doing some web automation via C# and a WebBrowser. There's a link which I need to 'click', but since it fires a Javascript function, apparently the code needs to be executed rather than just having the element clicked (i.e. element.InvokeMember("click")). Here's the href for the element, which opens an Ajax form:
javascript:__doPostBack("ctl00$cphMain$lnkNameserverUpdate", "")
I've tried:
webBrowser1.Document.InvokeScript("javascript:__doPostBack", new object[] { "ctl00$cphMain$lnkNameserverUpdate", "" });
and:
webBrowser1.Document.InvokeScript("__doPostBack", new object[] { "ctl00$cphMain$lnkNameserverUpdate", "" });
and a few other things. The code gets hit, but the script doesn't get fired. Any ideas would be most appreciated.
Gregg
BTW Here's the full element in case it's useful:
NS51.DOMAINCONTROL.COM<br/>NS52.DOMAINCONTROL.COM<br/>
Have 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 perfectly.
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");