Download File with WebBrowser Control On Click - c#

I need to download an excel file from a site.
I created a WebBrowser control inside a Windows Form that navigates to the site, select a criteria, clicks a search button which opens up another page.
Now on this page there is a link button with a javascript function, that downloads an excel file when it is clicked.
the link code is the following:
<a onclick="$find('ReportViewer').exportReport('Excel');" href="javascript:void(0)" style="color: rgb(51, 102, 204); font-family: Verdana; font-size: 8pt; padding: 3px 8px 3px 32px; display: block; white-space: nowrap; text-decoration: none;">Excel</a>
I wrote a code in the WebBrowser to find the link with Text "Excel" and click it, but this opens up Internet explorer and prompts the user for download.
This is an automated process, so I don't need the prompt, I need the download to happen automatically and save the file in a specific directory.
I tried to use this solution
Download a file through the WebBrowser control
But it doesn't work in my case, I think because I don't have a direct url that ends with pdf or xls. It's a javascript call.
Any ideas ?

HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned")) //replace this with how you search for excel
link.InvokeMember("Click");
}

Related

How do I read in and validate a csv file's raw data in asp.net

(* raw data as in the way it appears when you open a csv file using notepad as opposed to seeing columns in Excel)
I need to validate .csv files when they are uploaded to a web application, but I realized the information I need to validate does not appear in the csv file when i open it up in Excel, however I can see the data when I open the .csv file with notepad.
Here is a sample of the code that appears when I open a csv file in notepad:
<tr>
<td style="padding-left:10px;font-family: Verdana; font-size: x-small;"><br /><p style="text-align:center; font-family: Arial, Helvetica, sans-serif; font-size: small; color: #663399; font-weight: bold;">
<br />
<a href="http://www.google.com"></a>
</p><br /></td>
</tr>
The part i need to validate is
http://www.google.com which I believe i know how to do, the problem is how do I read the csv file's code in my application ?
Thank you
This is not a CSV file. Looks like HTML to me.
What you can do is simply read that file as text and then simply do the operations you want to perform.

Couple Things With Selenium

I've been doing some work with Selenium and Fiverr.com and I've run into some issues I was hoping to get some help on. I'm trying to create a new gig, and I've managed to do everything but a few things still are stumping me. One thing is the picture uploading.
• You have a button that you click a file input type () that brings up a Windows dialog, my plan thus far to use SendKeys.SendWait has worked, except for the part where you push "Open" on the Windows dialog to submit the picture. I've tried using SendKeys.SendWait("{ENTER}") to click it but that hasn't worked for me so far, so any ideas on that? I can't think of any other way to do it unless P/Invoke has something to handle Windows Dialog boxes?
•I'm also trying to enter text into the 'Description' box, and haven't been able to do that yet. I have tried to get it by class name, but it tells me that it can't access the class because it is hidden, have also tried to do it through Javascript (IJavaScriptExecutor) like so:
IWebElement detailFrame = firefoxDriver.FindElement(By.XPath("/html/body"));
var executor = firefoxDriver as IJavaScriptExecutor;
executor.ExecuteScript("arguments[0].innerHTML = 'hiiiiiiii'", detailFrame);
It's odd because they're using body for the text field like so:
<body marginwidth="0" marginheight="0" spellcheck="true" class="wysihtml5-editor" contenteditable="true" style="color: rgb(119, 119, 119); cursor: auto; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; letter-spacing: normal; text-align: start; text-decoration: none; text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px; overflow: auto; background-color: rgb(255, 255, 255);">TEXT HERE<span id="_wysihtml5-undo" class="_wysihtml5-temp"></span></body>
I think they're using two different 'bodys' though, but the XPath just shows that it's /html/body. How would I set the text for something like this?
Thank you guys for your help, been at this for the past two hours.
Selenium has mechanism for handling window. You can use driver.SwitchTo().Window(""); to set focus on newly opened window and perform additional action on it. However, keep that in mind to go back to parent window you may need to use default content again. See here for other window handling techniques.
First of all, selenium doesn't recognize hidden element. If you know for sure the element is not hidden then I might say taht the selector you are using probably not correct. If I understand the html you provided clearly and the element is visible then I suggest you use xpath for that element. //*[#id='_wysihtml5-undo']//.. can be helpful one for you

Internet Exploder submitting a null file from hidden iframe

I have a page with a jqGrid. On that grid I've added a custom button to the pager that, when clicked, opens a file browser for the user to upload an excel file. The file input is wrapped in a form and targeted to a hidden iframe.
I know that in Internet explorer, you cannot submit a file using javascript. I'm trying to figure out a way to submit the iframe and actually get the file to pass to the server with a value.
In chrome I'm able to submit it by triggering a click on a submit button I have way off the page on file input change and get the file, but in Internet Exploder it comes back null.
HTML:
<form action="/ManagedCatalog/ImportContractsFromExcelFile" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="">
<input type="file" id="uploadedFile" name="uploadedFile" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
<input type="submit" id="submitBtn" name="submitBtn" value="Upload" style="position: absolute; z-index: 19999; top: -1000px; left: -1000px;"/>
</form>
<iframe id="upload_target" name="upload_target" src="#" style="width: 0; height: 0; border: 0px solid #fff;"></iframe>
The custom nav button fires this:
function ImportContractsFromExcelFile() {
$('#uploadedFile').trigger('click');
}
And what I've got to work in Chrome:
$('#uploadedFile').change(function () {
$('#submitBtn').trigger('click');
});
Any tips, tricks, advice, reviews, or beatings are fully encouraged.
plz help.
Edit:
I'm doing it this way, because the silverlight app my team and I are rewriting acts this way and the client would like to keep this behavior. I may go down a jquery dialog window route and give the user a button to click, but I'd like to try and keep it as similar as possible.

display waiting dialog box while downloading the file in mvc4

I am generating excel file from sql database, sql datatable contain 50k data so that file took long time to download. so i want to display one waiting dialog box to user while downloading that file. Any suggestion?
you can try the way I did it using jquery. this will show a loading image during a long process.
Add a gif(loading image) in your _Layout.cshtml. Put this inside the body.
<div id="loaderblock"></div>
<div id="loadercontainer">
<img src="~/Images/sampleloadingimage.gif" />
</div>
add this to your Site.css
#loadercontainer { display:none;position: fixed;margin-top: 20%; margin-left: 45%; z-index: 999999;}
#loaderblock {background: #000; opacity:0.6; position: fixed; width: 100%; height: 100%; top:0; left:0; display:none;}
add this javascript in your view.
function generateexcel() {
$('#loaderblock').fadeIn();
$('#loadercontainer').fadeIn();
$("#idofyourform").submit(); //submit your form
}
and change your submit button to something like this:
<input type="button" value="Generate Excel" onclick="generateexcel();" />
this sample will show you how it works: sample
that is not my issuse.i want that loading image display only while the file is downloading.when file is successfully download and save dialog appear on browser at that time loading image should disappear automatically.

How to make a div into a Hyperlink?

I have problem in Visual Studio 2008 (ASP.net with C#).
I have div#apDiv13, now I want to make my div a hyperlink.
I set my div's background and now I want it to be a hyperlink on mouse over
or go to some address in web on-click.
This is very bad idea but technically it's possible. What you need to do is display link as a block and set its size to 100% vertically and horizontally so it fills the container div.
Example (http://jsfiddle.net/snLwg/)
div {
background: red;
width: 300px;
height: 300px;
}
div a {
display: block;
height: 100%;
width: 100%;
}
Simple way:
<a href="http://stackoverflow.com/">
<div>Contents of your div</div>
</a>

Categories

Resources