get and set attribute value for element in awesomium in C# - c#

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.

Related

C# Get Values from two different websites

I am using HTMLElementCollection, HtmlElement to iterate through a website and using Get/Set attributes of a website HTML and returning it to a ListView. Is it possible to get values from website a and website b to return it to the ListView?
HtmlElementCollection oCol1 = oDoc.Body.GetElementsByTagName("input");
foreach (HtmlElement oElement in oCol1)
{
if (oElement.GetAttribute("id").ToString() == "search")
{
oElement.SetAttribute("value", m_sPartNbr);
}
if (oElement.GetAttribute("id").ToString() == "submit")
{
oElement.InvokeMember("click");
}
}
HtmlElementCollection oCol1 = oDoc.Body.GetElementsByTagName("tr");
foreach (HtmlElement oElement1 in oCol1)
{
if (oElement1.GetAttribute("data-mpn").ToString() == m_sPartNbr.ToUpper())
{
HtmlElementCollection oCol2 = oElement1.GetElementsByTagName("td");
foreach (HtmlElement oElement2 in oCol2)
{
if (oElement2 != null)
{
if (oElement2.InnerText != null)
{
if (oElement2.InnerText.StartsWith("$"))
{
string sPrice = oElement2.InnerText.Replace("$", "").Trim();
double dblPrice = double.Parse(sPrice);
if (dblPrice > 0)
m_dblPrices.Add(dblPrice);
}
}
}
}
}
}
As one of the comments mentioned the better approach would be to use HttpWebRequest to send a get request to www.bestbuy.com or whatever site. What it returns is the full HTML code (what you see) which you can then parse through. This kind of approach keeps you from seinding too many requests and getting blacklisted. If you need to click a button or type in a text field its best to mimic human input to avoid being blacklisted also. I would suggest injecting a simple javascript into the page header or body and execute it from the app to send a 'onClick' event from the button (which would then reply with a new page to parse or display) or to modify the text property of something.
this example is in c++/cx but it originally came from a c# example. the script sets the username and password text fields then clicks the login button:
String^ script = "document.GetElementById('username-text').value='myUserName';document.getElementById('password-txt').value='myPassword';document.getElementById('btn-go').click();";
auto args = ref new Platform::Collections::Vector<Platform::String^>();
args->Append(script);
create_task(wv->InvokeScriptAsync("eval", args)).then([this](Platform::String^ response){
//LOGIN COMPLETE
});
//notes: wv = webview
EDIT:
as pointed out the absolute best approach would be to get/request an api. I was surprised to see that site mason pointed out for bestbuy developers. Personally I have only tried to work with auto part stores who either laugh while saying I can't afford it or have no idea what I'm asking for and hang up (when calling corporate).
EDIT 2: in my code the site used was autozone. I had to use chrome developer tools (f12) to get the names of the username, password, and button name. From the developer tools you can also watch what is sent from your computer to the site/server. This allows you to recreate everything and mimic javascript input and actions using post/get with HttpWebRequest.

Get the documentdata from a webbrowser control in another application

I'm looking for a way to get the document information (or document text) from another applications webbrowser control (and possibly alter it).
The other application is written in .net, but not by me.
I'm looking for an ability like this:
I would like an eventhandler for the OnDocumentCompleted that can get me the information of that document.
If possible, i would also like to intercept certain pages, add some html, and send them back to the second app to be displayed.
Searching the web pointed me towards using 'Hooks', but not much is found using hooks in this situation.
Hope you can help me out
Anthony
This code provides an example of html parsing that returns plain text (
the parsing depends on page content).
private string GetPlainText(WebBrowser webBrowser)
{
StringBuilder sb = new StringBuilder();
// Pick out a heading.
foreach (HtmlElement h1 in webBrowser.Document.GetElementsByTagName("H1"))
sb.Append(h1.InnerText + ". ");
// Select only some text, ignoring everything else.
foreach (HtmlElement div in webBrowser.Document.GetElementsByTagName("DIV"))
if (div.GetAttribute("classname") == "story-body")
foreach (HtmlElement p in div.GetElementsByTagName("P"))
{
string classname = p.GetAttribute("classname");
if (classname == "introduction" || classname == "") sb.Append(p.InnerText + " ");
}
return sb.ToString();
}
}

How to retrieve aspx parent name from IHTMLElement

I'm looking to get the *.aspx page name from the parent of an IHTMLElement. I started looking through the attributes on an IHTMLElement, and the document property looked promising.
Do I just need to cast as follows?
IHTMLElement elem;
elem = getElement(args);
IHTMLElement2 dom = (IHTMLElement2)elem.document;
string aspx = dom.<something?>;
That doesn't appear to work, but I feel like I'm on the right track. Ideas?
HTMLDocument doc = somedoc;
Regex pullASPX = new Regex(#"(?<=\/)[^//]*?(?=\.aspx)");
if (elem != null && !doc.url.Contains("default.aspx"))
{
EchoAbstraction.page = pullASPX.Match(doc.url).Value;
EchoAbstraction.tag = tagName;
EchoAbstraction.id = elem.id;
}
This is how I ended up doing it. I had found the ID in the dom already, so I just pulled the current doc page and parsed the URL.

C# WPF WebBroswer Control: How to use JavaScript

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"

Javascript works on IE but not on Firefox and gives me error as Error: cprofiledetailscollapse is not defined

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.

Categories