C# WPF WebBrowser button click - c#

I'm messing around with a WebBrowser in Visual Studio. I would like to create a button inside my main window that, when clicked, it clicks a button inside the WebBrowser. So basically I would just like to know how to press a button automatically using C#.
I have found several pages that tell me how to do this in WinForms, but I really want to do it in WPF and it seems like these things don't work there.

I don't really understand what you are trying to achieve (maybe you can try using another web browser or library for accessing html).
Anyway you can try to call javascript from WPF:
object[] codeString = {"alert('Replace this alert with javascript code you need');"};
object result = webBrowser.Document.InvokeScript("eval", codeString);

Related

Refresh White Automation Element Tree

I am using TestStack.White to do automation UI testing. A problem I am running into is that the elements tree is not updated after I click a button that brings up a new screen.
White works using UI Automation Verify(Microsoft tool), so if you cannot find an element using that tool, White will not be able to find it either.
If I open the app, click the button to render the view, and then open up UI Automation Verify, then all the fields in that new view show up in the tool. However, if I have UI Automation Verify open before I click the button, the new view does not show up in the tool. Hence, it seems I need to simply refresh the elements tree somehow.
Is there some way I can do this in C# so that my White testing will be able to see those new rendered elements?
I had the same problem (when switching from creating control to simply changing the visibility of existing controls).
Before that code worked:
checkButton.Toggle();
After that it didn't. The solution was to use
Mouse.Instance.Click(checkButton.ClickablePoint);
instead.
Somehow the TestStack/White does respond and refreshes UI better when you use Mouse object directly.
Have you tried InitializeOption.WithCache option?
Window modalWindow = mainWindow.ModalWindow(SearchCriteria.ByText("Modal Window"), InitializeOption.WithCache);
modalWindow.ReloadIfCached();
//or
modalWindow.ReInitialize(InitializeOption.WithCache);

How can I open a webpage first and then fill in text boxes and click a button and visibly view the results?

[edit] It is a requirement that the webpage spawn and open in IE and allow user manual interaction after the programmatic actions have completed.[/edit]
I've seen a lot of code examples online about opening webpages or filling in webpage textboxes and getting a return value without ever opening them visibly.
I would like to open a webpage in IE, fill in a few textbox buttons
and then click the submit button and view the results visibly.
I am able to do this with a dll called Selenium, but I do not want to use a 3rd party application and it seems that WebBrowser() should be able to do this?
I can post my failed code examples if that would help.
Thanks.
Maybe this qould fit better as a comment, but I don't have enoigh reputation.
Do you know how HTTP-Forms work?
It would probably be easier to send a HTTP-Request to the target of the form you want to fill, including the parameters you would like to fill into the form.
So you don't need any WebBrowser or similar, just a simple HttpWebRequest object, where you specity the target, the method (very likely POST) and the data you'd like to send.
You can use the webbrowser control in Winforms. It is possible to access every DOM object of the website using the control. No need to open the IE externally.
You just need to specify the webbrowser URL as your link.
Then, fill the textboxes with code,
BrowserID.Document.GetElementById("TextboxID").SetAttribute("Value", "NewVaue")
Also, you can click on the button using InvokeMember("click").
There are lots of stuff using WebBrowser. You can learn it here.

window.showModalDialog and whatever permutation of WebBrowser

Is there anyway to get access to or control the windows that pops up from JS calling window.showModalDialog?
I am currently using the WPF webbrowser which as I've noticed has very limited help in this area so I tried switching over to the Forms.WebBrowser version but that also doesn't seem to help me... I tried all the different permutations of newwindow events but they only fire when a completely new IE window pops up like from a _blank target...
The window that pops up has a series of check boxes I need to click through and then click OK from code
the best thing I've figured out as a plan so far is to inject some JS that emulates the return parameter of the dialog. But I was curious if there's a way to control or access this dialog from code...

Simulate keyboard on another web page from my web page?

I would like to display another web page within a web page... maybe using framnes, then be able to simulate mouse clicks/keyboard input randomally on this new page that is within my page.
Is this possible using asp.net/ javascript or any language. asp.net preferred
Thanks for any input
I dont think it is possible on client. If you are creating a desktop application then may be it could have been done like in automation, but on clint side even javascript cant create events like mouse clicks, it can only handle such events. however you can insert text in text boxes by traversing and accessing DOM in your javascript.
Its Definitely not possible from Server side in ASP.Net

c# save contents of IE browser as html

I have an Internet Explorer window open. The title of this window will always be "test123"
how do I save the source of the contents of the window as an HTML file?
Please note that the process should not be to open a URL and read the HTML into a variable. I absolutely HAVE TO do it the way I described since I need to login to a site to be able to view the HTML that I want to save.
**if it makes it easier to do this through my winform and putting a webbrowser control on it, that is fine as well.
You can attach to virtually any Windows app, using managed code and the UI Automation classes. Not a lot of people know about this stuff.
Microsoft shipped a class library and runtime that allows applications to automate other Windows on the system. You can do things like click buttons, read textboxes, activate menus, and so on. Here's a quick intro.
It should be relatively simple to attach to an IE Window, and then programmatically tickle the File...Save As... menu option.
I did this the other day for a Paint.NET app. It took much less time that I thought it would.
But I agree it's probably easier to use a WebBrowser control in a regular app, to programmatically retrieve content. You could also use the System.Net.WebClient class to do it, if you don't need to show the HTML content.
If you embed a WebBrowser control inside of your WinForm you can do this:
webBrowser1.Navigate("http://StackOverflow.com");
string pageHtml = webBrowser1.Document.GetElementsByTagName("html")[0].InnerHtml;
This will return all of the HTML in the page navigated to.

Categories

Resources