I'm trying to execute some piece of javascript in my awesomium WebControl wb.
An element exampleDiv shall be clicked when the user clicks on a button on my GUI.
private void button_Click(object sender, RoutedEventArgs e)
{
if (wb.IsDocumentReady)
{
wb.ExecuteJavascript("document.getElementById('exampleDiv').click();");
}
}
If I execute this piece of javascript in Chrome everything works fine.
If I execute this in awesomium nothing happens.
Simple things like alert('Hello'); works fine but I didn't get anything else to work.
I also found this article executing javascript in awesomium to click on a div but it didn't help too.
I'm using the latest awesomium build (1.7.3).
I'm not certain if the issue is the version of chrome loaded with 1.7.3 or if its something else however i think what you should be doing is this:
document.getElementById('exampleDiv').onclick()
I tested something similar to your example with 1.7.3 using the attached debugger and i got the error:
TypeError: Object #<HTMLSpanElement> has no method 'click'
That span has a wired click event on it, and i can trigger it using:
document.getElementById('mySpan').click()
with chrome, but had to use .onclick() in awesomium.
Testing the same in the current version of chrome it works just fine. My guess is its most likely chrome 18, but I do not have the time at the moment to install it and verify.
If you are using jQuery then you could also use $('#exampleDiv').click();
You can simply do this
dynamic submit = document.getElementById("exampleDiv");
submit.Invoke("click");
and this piece of code will be inside webcontrol -> loadingframeComplete section. Works 100% for me.
Related
The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click
I'm using the GreyBox js library to display a popup. To give some more general context I've rewritten a solution that was in VB.NET to C#. The code is essentially identical in both, just with the differing syntax used in both. However, the following works in the VB.NET solution but not the C# version:
VB
script = String.Format("GB_showCenter('My Caption', '../MyPage.aspx?number={0}&state={1}&ID={2}',300,600 );", num, MyLabel.Text, Label_id.Text)
ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), Guid.NewGuid().ToString(), script, True)
This works and when a button is clicked it navigates the user to a new page that has the size restricted. However, the following doesn't work.
C#
script = String.Format("GB_showCenter('MyCaption', '../MyPage.aspx?number={0}&state={1}&ID={2}',300,600 );", num, MyLabel.Text, Label_id.Text);
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), script, true);
What should happen is when I click a link button the text in the MyLabel is evaluated and if the text is the correct one then the string 'script' is set appropriately and registered with the scriptmanager. Running through with the VS2010 debugger this is all happening as expected. However, the user isn't navigated to a new page called 'MyPage.aspx'. The url stays the same and the page goes blank.
What's more interesting is that if I click the scroll bar the current page is briefly displayed along with the new popup shown above it on the same page. So my current theory is that it's something to do with how the javascript is called from the c#. Any ideas?
UPDATE
I went through with the vs2010 debugger and decided to see if MyPage.aspx was hit at all. It wasn't, as I expected. However, I then thought that perhaps it wasn't firing it's Page_Load event. So I added in:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Load += Page_Load;
}
I set a breakpoint on the Page_Load event of MyPage.aspx and this was now being hit. All the logic was being run through correctly, but I was still getting the same issue (blank page etc, url not changing to MyPage.aspx etc).
I found the answer, though it's not really related to GreyBox specifically. I'd incorrectly made something a script when it should have been a link and added incorrect attributes. The type was text/javascript when it should've been text/css. This seemed to make the difference. To give more context I had the following:
HtmlGenericControl Link5 = new HtmlGenericControl();
Link5.TagName = "script";
Link5.Attributes.Add("href", ResolveClientUrl("~/MyApp/Greybox/gb_styles.css"));
Link5.Attributes.Add("rel", "stylesheet");
Link5.Attributes.Add("type", "text/javascript");
Page.Header.Controls.Add(Link5);
The 'TagName' should've been 'link' and the Link5.Attributes.Add("type", "text/javascript") should've been 'text/css'.
I'm able to navigate to gmail, but then I want to do something as simple as enter the credientials and click the login button.
private void btnSubmit_Click(object sender, EventArgs e)
{
btnSubmit.Enabled = false;
webGmail.LoadURL("http://www.gmail.com");
webGmail.LoadCompleted += ExecuteSomething;
}
private void ExecuteSomething(object sender, EventArgs eventArgs)
{
webGmail.ExecuteJavascript(#"<script src = 'http://code.jquery.com/jquery-latest.min.js' type = 'text/javascript'></script>");
webGmail.ExecuteJavascript(#"$('#Email').val('foo');");
webGmail.ExecuteJavascript(#"$('#Passwd').val('bar');");
webGmail.ExecuteJavascript(#"$('#signIn').click();");
}
Nothing happens. I know using developer tools with Chrome that you cant modify anything on the page. But is there a way of filling in forms?
Are there any other better headless browsers? I actually need one that supports a web control that I can put into my form so that I can see what is going on. This is mandatory
The problem is that the script tag is not javascript - it's HTML - so executing it as javascript will just throw an error. To load a script with the ExecuteJavascript method, you'd need to create a script element in javascript and inject it into the page head.
See here for an example:
http://www.kobashicomputing.com/injecting-jquery-into-awesomium
I recently came across a similar problem. I tried cefsharp, awesomium, open-webkit-sharp, geckofx. The most advanced was, oddly enough, WebBrowser. It allows you to perform almost all activities directly with C#. For example, click on a submit button in C# you could only in WebBrowser. If you still want to use an alternative engine, I recommend the open-webkit-sharp - it is the most advanced of them (although it has the same problem with the click of buttons).
WatiN has an Javascript implementation for Webkit, which Awesomium is based on, the source code is free and can be downloaded at their homepage. Good luck.
Maybe this question could help you too, calling Javascript from c# using awesomium.
I have a c# asp.net page and an update function which will update the database. In this function I would like to call some client side javascript. I've read a lot about registering a start up script in page_load() but this is always trigger on page load (funny that!)
How would I register then call a script inside my update function? Triggered when a user clicks the "update" button. I have tried the following (inside my function)
protected void doUpdate(object sender, EventArgs e) {
string jScript;
jScript = "<script type=text/javascript>alert('hello');<" + "/script>";
ClientScript.RegisterStartupScript(GetType(), "Javascript", jScript);
}
but it isn't fired. Any ideas? Many thanks.
[update]
It's now working - the function looks like this
protected void doUpdate(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this, GetType(),"Javascript", "cleanup();",true);
}
Cleanup() is the javascript function in my HTML. Thanks for the help guys :)
If the control causing the postback is inside an UpdatePanel you need to use
ScriptManager.RegisterStartupScript
You can't 'execute' client side scripts from the web server (the client knows who the server is, but not the other way around).
The only way to overcome this limitation is by a. create a long-polling process that requests something from the server, the server doesn't complete the request till it has something to return (then client side it makes another request).
What you are really looking for is websocket (duplex) enabled communication. You can check out alchemy websockets or SignalR (has a pretty nice library with dynamic proxy generation).
The reason why that 'script always works on Page_Load' is because it effectively injects your script tag into the html returned for the page requested.
Your Update button is likely using the standard ASP Button behavior, meaning it is type="submit" when it is rendered. Since that's the case, you can just use:
Page.ClientScript.RegisterOnSubmitStatement
Keep in mind that will register a script for every postback, not just the Update button. So, if you only want some javascript run on clicking Update, you would need to check if the EventTarget is UpdateButton.ClientID. Also, RegisterOnSubmitStatement always adds the <script> tags, so don't include those in the javascript statement.
An even easier solution, the ASP Button itself also has an OnClientClick property. This will run client-side code (javascript) when the button is clicked in the browser.
How to refresh the parent window from child window close in asp.net (.cs page). We are using
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", "<script> JavaScript(`successfully Saved`); window.close();window.top.opener.RefreshPage();</script>");
for refresh the parent page from child window close.
Here RefreshPage() is a custom function
function RefreshPage() {
window.document.forms[0].submit();
}
It is working fine in IE but not in Mozilla Firefox and Google Chrome.
Update
Again it is not working with your solution also. My project structure is entirely different then what you explained above. We are calling the Java script functions in code behind by using page.ClientScript.RegisterStartupScript(). Here we are calling the different JavaScript functions separated by semicolon(;). See below
page.ClientScript.RegisterStartupScript(this.GetType(), "PopupSave", <script>javascript:alert('Successfully Saved'); window.close();window.top.opener.RefreshPage();</script>");
here RefreshPage() is the user defined JavaScript function i.e.
function RefreshPage()
{
window.document.forms[0].submit();
}
It is working fine in Internet Explorer but not working in Mozilla Firefox and Google Chrome.
In 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() function in IE it is firing but not in Firefox.
Perhaps you could use something like
window.parent.forms[0].submit();
or if you do not want to use the submit functionality you could use
window.parent.location.href = 'yourURL';
Maybe you should move the call to RefreshPage() before the window.close().
window.opener.location.href = window.opener.location.href;