I am trying to upload an attachment using Selenium (C#).
Upon checking the DOM of the site, I noticed that the link to attach files is using object tags.
Below is the HTML excerpt:
<object id="ctl00_mainContent_rauFilessilverlight03" class="ruObject" height="22px" type="application/x-silverlight-2" data="data:application/x-silverlight-2," style="width: 100%;">
<param value="/App/somelongjunkyparameters" name="source"/>
<param value="true" name="windowless"/> <param value="transparent" name="background"/>
<param value="some number" name="minRuntimeVersion"/>
<param value="PostData=anotherlongjunkyparameters,SilverlightRowId=ctl00_mainContent_rauFilessilverlight03,AsyncUploadId=ctl00_mainContent_rauFiles,MultipleSelection=Disabled,AllowedFileExtensions=,ServiceHandlerUrl=/App/Telerik.Web.UI.WebResource type=rau,MaxFileSize=0" name="InitParams"/>
<param value="true" name="autoUpgrade"/>
</object>
I have tried this so far:
IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[#class='ruObject']"));
fileAttachTA.Click();
String filePath = "C:/User/My Documents/file.txt";
Selenium was able to find the object, but, should I switch to the Windows Upload Dialog?
Hoping to hear from anyone who has experience in this.
Thanks!
I got it and what I did was this:
IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[#class='ruObject']"));
fileAttachTA.Click();
//Switch into the windows upload dialog
SendKeys.SendWait("^a");
Thread.Sleep(1000);
SendKeys.SendWait(file);
Thread.Sleep(1000);
SendKeys.SendWait(#"{Enter}");
Thread.Sleep(1000);
I used the System.Windows.Forms to get the SendKeys.SendWait to work. Thanks everyone!
Whoever developed the website is using a non-standard mechanism for uploading files. Looking at the HTML you provided, it looks like a Silverlight control of some sort. While Selenium WebDriver can properly handle the file selection dialog for uploading a file when a page is using a standard HTML upload mechanism (i.e., an <input type="file"> element), it has no hope of doing so with a non-standard upload mechanism. You'll need to find a way to handle the dialog outside of Selenium.
I've had issues with talking to a Windows dialog box when downloading/uploading files. My solution was to utilize user32.dll GetForegroundWindow(). Then created some wait methods for the dialogue box to appear disappear based on header text(still using user32.dll). Then finally created an action to BeginInvoke, waited for the window to pop up, and proceeded with send keys. Don't have code examples in front of me right now, but Google user32.dll Selenium and you'll get some info.
Related
I am attempting to upload a file to the application using SendKeys to the //input tag and it won't enter into it, because style is display:none
I also attempted to execute a javascript where I set the attributes of style to 'visibility: visible; display: block;'
This uploads the file, however, after few seconds the file disappears again. This happens when I interact with different element on the same page. I am not sure what is causing the file to disappear after. Any ideas?
Here is my selenium, C# code:
var el = Driver.Current.FindElement(By.XPath("//*[#id='fileUpload']"));
var js = (IJavaScriptExecutor)Driver.Current;
var script = "arguments[0].setAttribute('style', 'visibility: visible; display: block;')";
js.ExecuteScript(script, el);
//el.Clear();
el.SendKeys(#"PATH TO FILE");
Something to note, I have used different locators other than Xpath, I get the same results.
And here is my HTML:
And here how it looks in the app:
do you start interacting with other elements of the same page right after it is uploaded?
Give some time delay right after you upload. maybe use Thread.Sleep(100).
Also, to make sure it is uploaded correctly, try pressing enter right after you do el.SendKeys(#"PATH TO FILE"); maybe like this: el.SendKeys(Keys.Enter);
I'm trying the code below but its not working..
<object width="425" height="344">
<embed src="C:\Users\fortress\Desktop\AppointmentApp.swf" type="application/x-shockwave-flash" width="425" height="344"></embed>
</object>
Also tried this. Not working also.
<object width="425" height="344">
<embed src="~/Styles/Images/AppointmentApp.swf" type="application/x-shockwave-flash" width="425" height="344"></embed>
</object>
Problem:
The swf is not displaying.
I saw an error when I open the designer saying that "A control type was not specified or the specified type could not be found." I already import the shockwave-player for flash but still this error appeared..
I want also that the .swf will be fullscreen played in .aspx.
I'm newbie in ASP.net.. SO yeah, please explain also the code if its okay...
Here's the output in client-side:
Here's the whole code for my client-side:
Thanks!
This isn't ASP.NET, this is HTML. It might be served to the client by an ASP.NET server-side application, but that makes no difference to the client.
As far as HTML goes, your paths are broken. In both cases:
C:\Users\fortress\Desktop\AppointmentApp.swf
~/Styles/Images/AppointmentApp.swf
In the first case you're referencing a file system path. This wouldn't work on any client computer which doesn't have that file. If the file is on the web server then no client will be able to access the web server's C: drive. In the second case you're using a server-side relative path with a ~, and no client will be able to make sense of that.
When the page renders, the path needs to reference the file from the client's perspective. Something like this:
/Styles/Images/AppointmentApp.swf
Or perhaps:
../../Styles/Images/AppointmentApp.swf
Or whatever the path is from the rendered page to the SWF file.
I'm not 100% sure if this works well for object/embed tags, but you might be able to use the ~ path reference if you make the tag a server-side control. That should just be as easy as adding runat="server" to the tag:
<embed runat="server" src="~/Styles/Images/AppointmentApp.swf" type="application/x-shockwave-flash" width="425" height="344"></embed>
This would indicate to the ASP.NET application that the control needs some server-side processing before it's rendered to the client, and that processing would include evaluating relative paths.
You need both <param> movie tag and <embed>. Try this:
<object type="application/x-shockwave-flash" width="425" height="344">
<param name="movie" value="/Styles/Images/AppointmentApp.swf">
<embed src="/Styles/Images/AppointmentApp.swf" type="application/x-shockwave-flash" width="425" height="344"></embed>
</object>
Good luck!
We want to reduce the number of steps it takes for a user to upload a file on our website; so we're using jQuery to open and postback files using the below markup (simplified):
<a onclick="$('#uplRegistrationImage').click();">
Change profile picture
</a>
<!-- Hidden to keep the UI clean -->
<asp:FileUpload ID="uplRegistrationImage"
runat="server"
ClientIDMode="static"
Style="display:none"
onchange="$('#btnSubmitImage').click();" />
<asp:Button runat="server"
ID="btnSubmitImage"
ClientIDMode="static"
Style="display:none"
OnClick="btnSubmitImage_OnClick"
UseSubmitBehavior="False" />
This works absolutely fine in Firefox and Chrome; opening the file dialog when the link is clicked and firing the postback when a file is selected.
However in IE9 after the file upload has loaded and a user has selected a file; insteaed of the OnChange working I get a "SCRIPT5 Access is denied" error. I've tried setting an arbitrary timeout, setting intervals to check if a file is given to no avail.
There are a number of other questions relating to this; however none appear to have a decent answer (One said set the file dialog to be transparent and hover behind a button!)
Has anyone else resolved this? Or is it absolutely necessary that I provide a button for IE users?
For security reasons, what you are trying to do is not possible. It seems to be the IE9 will not let you submit a form in this way unless it was an actual mouse click on the File Upload control that triggers it.
For arguments sake, I was able to use your code to do the submit in the change handler, but it worked only when I clicked the Browse button myself. I even set up polling in the $(document).ready method for a variable set by the change handler that indicates a submission should be triggered - this didn't work either.
The solutions to this problem appear to be:
Styling the control in such a way that it sits behind a button. You mentioned this in your question, but the answer provided by Romas here In JavaScript can I make a "click" event fire programmatically for a file input element? does in fact work (I tried in IE9, Chrome v23 and FF v15).
Using a Flash-based approach (GMail does this). I tried out the Uploadify demo and it seems to work quite nicely.
Styling a File Upload:
http://www.quirksmode.org/dom/inputfile.html
http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
References:
jQuery : simulating a click on a <input type="file" /> doesn't work in Firefox?
IE9 file input triggering using Javascript
getting access is denied error on IE8
Hey this solution works.
for download we should be using MSBLOB
$scope.getSingleInvoicePDF = function(invoiceNumberEntity) {
var fileName = invoiceNumberEntity + ".pdf";
var pdfDownload = document.createElement("a");
document.body.appendChild(pdfDownload);
AngularWebService.getFileWithSuffix("ezbillpdfget",invoiceNumberEntity,"pdf" ).then(function(returnedJSON) {
var fileBlob = new Blob([returnedJSON.data], {type: 'application/pdf'});
if (navigator.appVersion.toString().indexOf('.NET') > 0) { // for IE browser
window.navigator.msSaveBlob(fileBlob, fileName);
} else { // for other browsers
var fileURL = window.URL.createObjectURL(fileBlob);
pdfDownload.href = fileURL;
pdfDownload.download = fileName;
pdfDownload.click();
}
});
};
This solution looks like it might work. You'll have to wrap it in a <form> and get it to post in the jquery change handler, and probably handle it in form_load using the __eventtarget or and iframe or whatever it is that web forms uses, but it allows you to select a file, and by submitting the form, it should send it. I can't test it however, since I don't have an environment set up at home.
http://jsfiddle.net/axpLc/1/
<a onclick="$('#inputFile').click();">
Change profile picture
</a>
<div id='divHide'>
<input id='inputFile' type='file' />
</div>
$('#inputFile').change(function() { alert('ran'); });
#divHide { display:none; }
Well, like SLC stated you should utilize the <Form> tag.
First you should indicate the amount of files; which should be determined by your input fields. The second step will be to stack them into an array.
<input type="file" class="upload" name="fileX[]"/>
Then create a loop; by looping it will automatically be determined based on the input field it's currently on.
$("input[#type=file]:nth(" + n +")")
Then you'll notice that each file chosen; will replace the input name to the file-name. That should be a very, very basic way to submit multiple files through jQuery.
If you'd like a single item:
$("input[#type=file]").change(function(){
doIt(this, fileMax);
});
That should create a Div where the maximum file found; and attaches to the onEvent. The correlating code above would need these also:
var fileMax = 3;
<input type="file" class="upload" name="fileX[]" />
This should navigate the DOM parent tree; then create the fields respectively. That is one way; the other way is the one you see above with SLC. There are quite a few ways to do it; it's just how much of jQuery do you want manipulating it?
Hopefully that helps; sorry if I misunderstood your question.
I've created a windows application with a WebBrowser control and trying to play youtube videos.. I've installed Adobe Shockwave Player, and tried to watch video on my windows application, but it doesn't show any videos. When I hit play button the screen looks like this
My code for playing video is
StreamWriter sw = new StreamWriter("utube.html");
string PlayString = currentVideoUrl.Trim().Replace("watch?v=", "v/");
string Finalplaycode = "<embed src=" + PlayString + "&hl=en&fs=1& type=application/x-shockwave-flash allowscriptaccess=always allowfullscreen=true width=425 height=344></embed>";
sw.Write(Finalplaycode);
sw.Close();
string PathToNavigate = Directory.GetParent(Application.ExecutablePath) + #"\utube.html";
webBrowser1.Navigate(PathToNavigate);
and my example video link is : http://www.youtube.com/watch?v=oZdnezj9Dfg&feature=BFa&list=PLD3E900BFF6534896&lf=plpp_video
can anyone help me about this issue? Did I missing any plug-in or something? I am using .NetFramework 4.0
One problem I see is that you're not quoting the parameters in the embed. Your embed will generate as:
<embed src=http://www.youtube.com/v/video_id...>
You need to quote those parameters:
<embed src="http://www.youtube.com/v/video_id...">
For example, here's an embed that works on my site:
<embed type="application/x-shockwave-flash" width="425" height="344"
src="http://www.youtube.com/v/UejelYnVI3U&hl=en&fs=1"></embed>
You might be interested in reading the official YouTube documentation. They've moved away from using embeds, in favor of iframes. That lets them use the HTML5 video player if it's available, or otherwise fall back to Flash. See http://www.google.com/support/youtube/bin/answer.py?answer=171780 for details.
Update:
The HTML file below embeds the same video in two ways: once using the iframe and once using the standard embed. It works find when I navigate to it with the WebBrowser control:
<html>
<body>
<iframe class="youtube-player" type="text/html" width="640" height="385"
src="http://www.youtube.com/embed/oZdnezj9Dfg" frameborder="0">
</iframe>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="src" value="http://www.youtube.com/v/oZdnezj9Dfg&hl=en&fs=1" />
<embed type="application/x-shockwave-flash" width="425" height="344"
src="http://www.youtube.com/v/oZdnezj9Dfg&hl=en&fs=1">
</embed>
</object>
</body>
<html>
Try copying that and navigating to it in your application. Make sure that your program is creating the embed just like that.
Also, you can remove the outer <object> tag if you already have Flash installed. Then, the Flash embed looks just like what I showed originally.
And, there's no requirement that you have the HTML wrapper around it. It works fine if I just include the embed without the <html> and other tags.
For learning purposes I would like to automate some parts in a browser game, currently I am trying to fill out some simple text boxes, without any luck though. I've created a WebBrowser component on my form, loaded the website via it and tried this.
webBrowser1.Document.GetElementById("citizen_name").SetAttribute("", "myname");
When I click my "fill out text box" button nothing happens. The HTML part looks like this:
<input type="text" name="citizen_name" id="citizen_name" value="" class="field" tabindex="1" />
I am talking about the eRepublik.com game, appreciate any help.
Try this:
HtmlDocument document = this.webBrowser1.Document;
document.GetElementById("citizen_name").SetAttribute("value", "myname");
I usually take the following approach:
var someElem = webBrowser1.Document.GetElementById("some_id");
if (someElem != null)
{
someElem.InnerText = "Some value";
}
Some textareas of advanced editors cannot have their value set this way. To handle them I do something like the following:
someElem.Focus();
Windows.Forms.SendKeys.SendWait("Some value");
For this kind of problems there are much more suitable environments.
The easiest to use is definitely userscripts.
What exactly do you want to learn? How to test web apps, or how to develop them?
You can use of course simple javascript that you include in your page, or better yet, using Greasemonkey , so you don't modify the "client" code.
But greasemonkey would only be an option for Firefox, Opera and Chrome. If you really need a full blown cross browser automation test suite, you could use Selenium IDE , that allows to record or script a series of interactions with a web page, that can be automatically run in any of its supported browsers.