I have a web application that does a bunch of things, and need to write a C# program to print a document that is generated by the system.
When a user clicks the print button on the web form (window.print()), it will print to a specific printer without popping up the print dialog box.
I tried webbrowser.print(), but it only prints the web interface without the pop up.
One of the things that I'm looking for is a way to handle a print job created by the website. I want the C# program to be able to remove the print dialog box and print directly to the printer.
Can anyone give me a hint as to how I can go about implementing this, or explain why (if) this isn't possible?
P.S. I cannot change anything in the web application. That is why I want to write a C# program to perform/manage this additional action.
you have a webbrowser control on the c# form? do this:
myBrowser.Document.GetElementById("ButtonId").InvokeMember("click");
it automatically click the button.
if the button does not have id, then you will need to use either its class or tag to identify that button. Search "click button automatically in webbrowser control".
Try injecting script inside the web page from C# code
Something similar to on some C# app button click . .
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function printDocument() { window.print(); }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("printDocument");
or simply
webBrowser1.InvokeScript("eval", "window.print();");
Related
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.
I am working on a project which is Analysis of Papers from Google Scholar. What I do is basically, parsing the HTML, storing related fields into database etc. However, I am stuck at a point, while I am taking the Titles of the publications, I realized, I am able to get first twenty elements. But, there are sixty papers in related account:
http://scholar.google.com/citations?user=B7vSqZsAAAAJ
So, I think as a solution, I need to click to the 'show more' button programmatically, so I can have all the Title's, Publication Venue etc.
What do you think? How can I perform that kind of action?
Edit: I checked the 'show more' button, while there is nothing to show as a next page, its html code still remains same. As a solution I can use loop for n times. However, I am looking for more robust solution.
Thank you for your time!
If it is clicking on a button within a WebBrowser control on a Windows Form Application, then 'Yes' you can do it.
There are ways of getting more control over identification by using XPath.
(You might need to use Javascript to use XPath for object interactions - since you haven't asked for that, I will assume you don't need it)
webBrowser.Navigate("http://www.google.com");
// Or
HtmlElement textElement = webBrowser.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "your text to search");
HtmlElement btnElement = webBrowser.Document.All.GetElementsByName("btnG")[0];
btnElement.InvokeMember("click");
Or even typing into text boxes with
webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";
If its this website specifically, there is a simple workaround. Change the query string to what records you want.
http://scholar.google.com/citations?user=B7vSqZsAAAAJ&cstart=0&pagesize=2000
I have an HTML page with this script:
<Script>
function Run()
{
alert("The Content Here");
return true;
}
</script>
<button onclick="Run();">I Want It Now</button>
Lets say that I opened this page with firefox or Chrome. And lets say that I clicked on the button "I Want It Now" and the page shows me the alert:
The Content Here
How can I insert the alert content into a string in my VB.NET project? I know that I can use the alert window handle to get the label handle and then extract (grab) the text of the alert, but I don't think that this is the best way to do it. Is there another way to pass (or get) information from the page (not from webbrowser control or by using webClient.DownloadString) into my VB.NET (or C#) project?
i don't this it will easy to use js without the webbrowser control by the way if you changed your mind and want to use a web browser control , you can do it like this :)
WebBrowser1.Document.GetElementById("NAME").InvokeMember("click");
I'm just using watin to fill some textfields but sometimes it writes in the wrong textfield because the textfields name is not clear here's my code
IE browser = new IE(site);
browser.TextField(Find.By("type","text")).TypeTextQuickly(username.ToString());
browser.TextField(Find.By("type", //"password")).TypeTextQuickly(pass.ToString());
browser.Button(Find.By("type", "submit")).Click();
Edit your HTML code and setup ID´s for your input elements. Then use Find.ById
One option could be to see if there is an outer element (such as a Div) you can find first, and afterwards get the text fields from that element instead of from the browser variable. That could for instance look like this:
Div div = browser.Div(Find.ById("divId"));
//Div div = browser.Div(Find.ByClass("divClass")); // or like this for instance...
TextField text = div.TextField(Find.By("type", "text"));
TextField password = div.TextField(Find.By("type", "password"));
Button submit = div.Button(Find.By("type", "submit"));
I am unclear as to your knowledge of WatiN and testing so I will start from the begginging. First you need to go on the webpage you want to test and (in IE) go to tools -> Developer tools. Click the white arrow in the menu then proceed to click the box you wish to utilize. Once you do this the developer tools will give you the code for the textbox including many ways you can reference it in your code. For example, using dev tools in IE I can automate logging into my gmail like this:
IE browser = new IE("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1<mpl=default<mplcache=2");
browser.TextField(Find.ByID("Email")).TypeText("myemail#email.com");
browser.TextField(Find.ByID("Passwd")).TypeText("mypassword");
browser.Button(Find.ByID("signIn")).Click();
There are many Find.By commands so there is no reason you can't acess ANY textfield you wish. You just need the html and to be specific to which one you want to write into. I hope this helps :)
I'm using the WebBrowser control and want to bypass a button press on a web page. I'm not very familiar with HTML and web pages, but I'm wondering if anyone has a solution.
The button I'm talking about is on this web page:
http://www.movshare.net/video/ut55cfdvg5wgj/?
The button is appearing at random so it might not be there always.
Edit: Thinking about this, you can do most of this from the C#. Updated.
Get a reference to the button and invoke it's click() JavaScript method:
HtmlElement btn = myBrowserControl.Document.GetElementById("myButton");
/*
Alternatively, take a look at these other methods for retrieving an HtmlElement:
HtmlDocument.GetElementFromPoint(Point point)
HtmlDocument.GetElementsByTagName(string tagName)
HtmlDocument.All.GetElementsByName(string name)
*/
btn.InvokeMember("click");
This will only work in all browsers if ...
Since this is a WebBrowser control, you don't need to worry about cross browser issues.
Do you want the user to skip the button press everytime. Then this means, you don't need the button at all on the page. Please put more information