executing javascript in awesomium to click on a div - c#

I'm using Awesomium in C# winforms app to click on a div inside the webpage.
the following script works just fine if you type it directly in google chrome:
javascript: document.getElementById('ac_play').click();
but when I try to execute it in awesomium using either:
webControl1.ExecuteJavascript("document.getElementById('ac_play').click();");
or this:
webControl1.LoadURL("javascript: document.getElementById('ac_play').click();");
It's not working. Which makes me think - does Awesomium support "div clicks" or not? Or maybe there is another reason why it's not working?
I've also tried to execute the code:
on LoadCompleted event (to make sure that the page is loaded completely)
by typing the script manually in "addressBox1" (native awesomium "address bar" control)
As usual - nothing is working.
EDIT:
I've tested the same javascript in GeckoFX and it's not working there either.. Any workaround?
EDIT2
Standart WebBrowser control executes the script perfectly! (Although it's using IE5 or so.. that's why I'd like to see Awesomium solution working).
P.S. it was working in webcontrol and now it's not ;( how do i reset it?
P.P.S. i removed the cache in IE but it's sooo unreliable to use standard WebBrowser..

Remember that you have to wait for DocumentReady event with DocumentReadyState as Loaded (not Ready - because then Awesomium is not quite ready yet)
private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
{
if (e.ReadyState != DocumentReadyState.Loaded) return;
//Here ExecuteJavascript should work
}

public void JsFireEvent(string getElementQuery, string eventName)
{
webControl1.ExecuteJavascript(#"
function fireEvent(element,event) {
var evt = document.createEvent('HTMLEvents');
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
element.dispatchEvent(evt);
}
" + String.Format("fireEvent({0}, '{1}');", getElementQuery, eventName));
}
JsFireEvent("document.getElementById('ac_play')", "click");
Link

Related

WEC7 web browser control not displaying page

I'm struggling with the browser control in my WEC7 application. I'm trying to display a local file in the webBrowser control, and everything appears to be working except that the page does not show up in the control on my form. All I see is a white rectangle where the webBrowser control is.
I made a stand-alone test app which does nothing but load a local file into the webBrowser control. I found code elsewhere on stackoverflow which seemed pretty clear. This is the code that loads the page:
private void LoadPageBtn_Click(object sender, EventArgs e)
{
try
{
string applicationDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetNam‌e().CodeBase);
string myFile = Path.Combine(applicationDirectory, #"HTMLPage1.htm");
Uri uri = new Uri(myFile);
webBrowser1.Navigate(uri);
}
catch (Exception ex)
{
Debug.WriteLine("ERROR: " + ex.Message);
}
}
The file HTMLPage1.htm is really basic and shows up fine in any desktop browser. If I provide a bogus file name in the code then I get a file not found exception, so I'm pretty sure the file is being deployed correctly on the target (set to "always copy" in the file properties).
I catch the Navigating, Navigated, and Complete events from the webBrowser control and output some debug stuff, including the URL from the WebBrowserNavigatingEventArgs. When the code runs I get the following debug output:
Navigating: file:///Program Files/webtest/HTMLPage1.htm
Complete:
I never see the Navigated event but I'm not sure that's a problem.
And the darn webBrowser control continued to show a white rectangle. Can anyone suggest what I may be missing?
I have posted the code here in case anyone would be kind enough to try it out themselves:
https://drive.google.com/file/d/0B75fBmfP8FI4YmpvYXFXcGN1Qzg/view?usp=sharing
OK, I found out what's going on. The answer was actually found on the Toradex support forum. The WebBrowser control is just a wrapper around IE and therefore IE needs to be included in the OS before you can use the WebBrowser in your application.
My OS build does not include the web browser components, so I'm in the process of rebuilding the OS and including all the IE7 stuff.

Stop multiple WebBrowsers from starting yt-videos?

I'm working with a winform TabControl showing WebBrowsercontrols to display youtube videos.
However with two videos or more it becomes really annoying as all videos start directly.
I basically need to find out if there is a JS function, html code or a simple WebBrowser property to change, so videos are paused.
It might come in handy to find something like that for video quality too.
Has anybody ever heard of/seen where this option is stored? Or maybe the Js function itself being invoked when manually setting the quality?
EDIT:
b.DocumentCompleted += delegate { b.DocumentText=b.DocumentText.Insert(b.DocumentText.IndexOf("class=\"video-stream html5-main-video\""), "autoplay=false ");};
b.Url = new System.Uri(inp[s], System.UriKind.Absolute);
Basically this should add a new Event handler on each webbrowser form that modifies the DocumentText when the Uri that is called during creation has loaded.
Even though the browser debugger shows
<video tabindex="-1" class="video-stream html5-main-video" controlslist="nodownload" style=... src=...></video>
this isn't in the actual source code.
However I found
$oa=function(a){g.S(a.o,"video-stream");g.S(a.o,"html5-main-video");var b=a.app.g;b.zc&&a.o.setAttribute("data-no-fullscreen",!0);b.Oh&&(a.o.setAttribute("webkit-playsinline",""),a.o.setAttribute("playsinline",""));b.Nr&&a.o&&a.P(a.o,"click",a.o.play,a.o)};
in the base.js. Is it possible that youtube generates the html from the js?
How can I modify the video-tag attributes then?
I tried to modify when the event handler manipulates the video tag, since there may be DocumentCompleted events thrown from scripts or anything.
delegate (object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e){
if (e.Url.AbsolutePath != ((System.Windows.Forms.WebBrowser)sender).Url.AbsolutePath){
//...
However it still fails as there is no occurance of the specific class that on the video tag.
I now dodged this by loading the Url only when the browser tab is selected, if someone finds a real solution, feel free to share

Communication between Webbrowser control and C#

Can a Webbrowser control communicate with the C# class that created it? Is there anyway to set up something like ExternalInterface that works in Flash? I'm trying to get the following code to write to the console in C# or call a method in C#.
HtmlElement button = webBrowser1.Document.CreateElement("div");
button.InnerHtml = #"<INPUT TYPE='Text'><P><INPUT TYPE='Submit' Value='Submit' onclick='console.log('Clicked');'>";
webBrowser1.Document.GetElementById("myID").AppendChild(button);
A pretty hacky way would be to communicate via current location.. Button click:
onclick="window.location.href='#clicked';"
Then handle webBrowser1_Navigated and check the Url property. Split out the message after the # and log it to the console or call the method.

WebBroswer_DocumentCompleted event is not working

I have a form with a browser control. (this control uses IE9 because I set values on registry editor)
This web browser navigates to a specific URL and fills all fields on HTML page and submit them, then result page is displayed.
My problem is that i just want to know when this reslut page is fully loaded or completed so that i can fetch some information.
I use WebBroswer_DocumentCompleted event which works fine for the first page but not for the result page as it triggers before result
page is loaded.
I tried other solution which is to check the div tag inside the result page (this tag only appears when result page is loaded completely) and it works but not always.
My code:
private void WebBroswer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection elc3 = this.BotBrowser.Document.GetElementsByTagName("div");
foreach (HtmlElement el in elc3)
{
if (el.GetAttribute("id").Equals("Summary_Views")) //this determine i am at the result page
{
// fetch the result
}
}}
That div id is "Summary_Views".
I can provide you the link of that website on demand which is just for BLAST tools and database website for research purpose.
Frames and IFrames will cause this event to fire multiple times. Check out this answer:
HTML - How do I know when all frames are loaded?
Or this answer:
How to use WebBrowser control DocumentCompleted event in C#?
Or ms's kb article:
http://support.microsoft.com/kb/180366
Do you know if there are frames? If so then please say so, so people can help with that. If not then say so, so people can offer alternatives.
My guess is that the content is being generated by JavaScript. If it is then the document is complete before the JavaScript executes and you need to somehow wait until the JavaScript is done. The solution depends upon the web page. So you might need to process multiple document completes for diagnostic purposes and attempt to determine if there is a way to know which one you need.
At last i have solved my problem. I put a timer control from toolbox and set its time interval to 200ms and its Autoreset property to false. I set a tick event which has a code to check every 200ms whether this Div has been loaded or not, after that, Autoreset property is set to true.This solution is working perfectly :)

how to refresh parent window by using Javascript in .cs

How to refresh the parent window while closing the popup window(child window).
We are calling the java script functions in code behind to refresh the parent window by using page.ClientScript.RegisterStartupScript().But t is working fine in IE(internet explorer) but not working in Mozilla Firefox and Google Chrome.
In the Mozilla Firefox the pop up value is saving in the database but it is not updating into the parent page.If i did refresh manually the value is getting updating into the parent page. If i put debugger in RefreshPage()(javascript function) function in IE it is firing but not in Firefox.
The below code for call the javascript function in .cs class.
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", "<script>javascript:alert('" + dsMessage.Tables[0].Rows[0]["ErrorMessage"].ToString() + "');window.open('','_self','');window.close();window.document.forms[0].submit();</script>");
The above code RefreshPage() is the javascript function to refresh the page
i.e.
function RefreshPage() { window.document.forms[0].submit(); }
Please help me i tried with different scenarios but no output.
instead of RefreshPage() i used different functions
like reload(),
window.opener.forms[0].submit(),
likewise but still no output anyone knows please help me.
Try this function
on submit button click run this script
<script language='javascript'> window.opener.frames.location='somepage.aspx';window.close();</script>
this will help you !!!
Before trying to make js calls between windows.
Set 'document.domain' to the same value on both pages.
protected void ButtonClick(object sender, EventArgs e)
{
string closeAndRefreshScript = #"<script type='text/javascript'>
window.opener.location.reload();
window.close();
</script>";
base.Response.Write(closeAndRefreshScript);
}

Categories