I'm trying to control a browser using C#. The way that used to work was OLE/interop. This seems to be missing from modern browsers. All I need it to do is be able to navigate to a URL and then change the URL from time to time. The problem using Process.Start("http://example.com") is this usually spawns several children processes and I can't determine the exact process to kill later. Calling it several times simply creates new children. I tried using the WebBrowser and WebView controls. Yes, these give me the control I want, but the rendering engine is embedded in the application and is so old, no sites allow these controls to work anymore.
I found something called WebDriver. At first this seemed like a good solution, except it appears to be very dependent on the exact build of the browser and has dependencies on Selenium. It probably works great for automated testing, I'm not convinced it will work well for long term browser control if the browser updates itself.
Next comes CefSharp. I've used this in the past, but it also seems to have an embedded and outdated rendering engine and many sites complain it too is out of date. And even if CefSharp gets updated from time to time, I need to rebuild my application each time. The problem I see here is, the rendering engine is embedded into an assembly that gets distributed with my app. Yes, I've seen a huge list of other embedded solutions. Many of them haven't been updated in years.
Ideally, I could control a browser using something like WebDriver that worked on any browser version. I could launch an external browser like FireFox or Chrome. Do any browsers have an API? Here's exactly the pseudo code I'd like to implement using a modern browser:
ModernBrowser br = new ModernBrowser();
br.Navigate("about:blank"); // clear out last page
if(some_flag)
br.Navigate(Url); // navigate to new page.
If(done)
br.Close();
br = null;
That is all I need. Dead simple.
I'm trying to implement a media playback system. All the code works fine except where I need a modern browser to play back Pandora, Spotify or any other web based systems. They all require the latest browser versions or they simply don't work. Embedding a rendering engine in my application is a nightmare. Sure, I can get it to work today. I'm just not sure about tomorrow.
I decided to try CefSharp and it works. I realize I'll have to update this from time to time. I want to point out that if you decide to use CefSharp as an embedded browser, don't use NuGet on your live project. Create a throw-away project and install the CefSharp assemblies. As of the date of this entry, the Nuget install breaks your C# project and makes every assembly unusable. What I did was I created a throw-away project, added the CefSharp assemblies and then took the dependent binaries and put them in a new folder. I made my live project an X86 target and that works.
Related
Problem
I recently replaced a System.Windows.Forms.WebBrowser-based control with one that relies on Microsoft.Web.WebView2.WinForms.WebView2 instead. This has been working well, except that my users want to have 2 instances of their application running at once, with both instances occasionally having that WebView2 form open at the same time. When they try this, one of the two instances fails to load the WebView2 control until the other one is closed.
Learned / attempted so far:
I've found WebView2 to be extremely finicky, so I've looked into alternatives like CefSharp, but haven't wanted to dedicate time to implementing them until I know they can solve the problem. So far as I can tell, CefSharp and WebView2 do very similar things as far as launching a Chromium-based browser, rendering to a WinForms control.
This blog post (read: advertisement) for paid alternative DotNetBrowser indicates that
You can initialize and use several Chromium engines simultaneously with different configurations [in DotNetBrowser], which is not possible in CefSharp.
I'm wondering if this same limitation applies to WebView2, and is the cause of my users' inability to have 2 instances of that same form loaded at once.
Context
I'm supporting an ancient desktop CRM that's set to be retired in favor of a modern alternative, but in the mean time, the old standard needs to integrate with new processes we're bringing in. One of those new processes is a web page from an external service that our call center users enter data into, then expect the CRM to pull entered data out of the web page.
The part of their process I did not know about is the 2-instance bit from above: they're used to being able to copy-and-paste from one embedded browser window to the other.
#Poul Bak in the comment on the question had it right: providing settings that differ per-instance of the application fixed the problem.
The setting I changed per-instance was the path of the user data folder: I was able to pass different values for the userDataFolder argument of the CoreWebView2Environment.CreateAsync method, branching based on a variable that holds different values between the instances that my users are running.
I have a very strange situation here with CEF Sharp x86 (67 with latest cef.redist 3.3497.1841), but originally reported with v49. It's very crazy, so I'm just looking, maybe somebody have stumbled upon anything similar. We host WPF ChromiumWebBrowser inside WinForms UserControl (via ElementHost) and it's running inside 3rd party application, with which it does not interact in any way (so it's a plugin to this host application). For about 100 or so users everything is ok, but for a single person this happens: webpage loads and displays correctly, but the ChromiumWebBrowser.IsBrowserInitialized flag is always false.
Now, by 'user' I don't mean Windows account, just a user in totally unrelated 3rd party application, logging-in against 3rd party userid/password verification - unrelated to any windows settings. So as crazy as it sounds we've tried it on multiple computers on multiple windows accounts, using different user ids for the application, and the result is still the same - for this one userid there is a problem, for others - all ok.
I wonder if anyone has had such crazy experience with CEF. I'm betting on some bug in .NET layer, as the actual browser works fine - webpage is rendered, you can browse normally. Also this has sth to do with CEF run as plugin to this exact 3rd party app - if I run a test winform app with same setup - user control -> element host -> WPF ChromiumWebBrowser, all is ok. CEF log is not much help, no errors other than in OK situation.
I'm not attaching any code or logs, I don't think it'd be helpful in this case. Just for more background the plugin has been around for almost 3 years with not a single bug reported, except this case. Just to make it clear - I need this flag to be true to be able to inject JS calls into the plugin.
Ok, so it turned out to be a case of opening 2 instances of browser within single process. One browser window was initializing and working fine, second was was displaying page, but reporting 'not initialized'. It became clear only when I went to customer and actually saw the scenario, because they did not report that they opened 2nd instance. I this case, they don't need 2nd instance, so that was the fix, maybe there is an error I our code, I'll follow up this issue once we do more through analysis.
I want to monitor changes in background in complex web application. This is one-page application with many scripts and so on. I need to be logged in to have access to data I want to monitor.
I tried to use webrequest, but I think that the application is to complex to do it that way. There is also a problem with authentication.
I also tried WebBrowser component, but web application is telling me, that this browser is too old and I should get newer one.
Perfect solution would:
Open this web application in chrome (or some other modern browser) in background
Save the page to memory
Extract values using something like HtmlAgilityPack
While this will be happening I want to normally use the computer (so opening chrome window is not a good solution for me).
Is there any way to achieve something like that?
if you can cope with an extra browser running, have a look at SeleniumHQ. with its webdriver-backed selenium you can start a dedicated browser instance and perform user actions by coding in high-level programming languages like java. it should not interfere your manual work at all, but will take up the same amount of memory and cpu time your "real" browser would.
if the web application has no captcha and does not object to automated script accessing it, you could also login in a background program by sending appropriate HTTP requests and parse the response. python's urllib2 would be my first choice.
if you dont want any additional processes running, you could also create a browser plugin, that autorefreshs and parses a certain open tab every few seconds.
I've looked through several similar questions on SO but haven't found something quite like what I need, so my question is this:
I want to take a screenshot (thumbnail) of a URL after the user provides one. I was going to use Awesomium because they provide a fairly simple solution for screengrabs. Unfortunately, Awesomium won't compile in an x64 application, and since I'm building this with ASP.NET for Windows Azure, I can't switch to x86.
So I'm left with a less-elegant solution, using Windows.Forms WebBrowser to load the url and take the screenshot (as shown here: http://www.codeproject.com/Articles/95439/Get-ASP-NET-C-2-0-Website-Thumbnail-Screenshot ).
Ugly, I know, but it works with most pages (there is the occasional white screenshot), but now I'm concerned with security.
If the user inputs a malicious URL and the WebBrowser loads it, what is to stop it from running harmful code and downloading a virus to the server where the app is hosted?
There are several services and websites that offer similar functionality, albeit with different approaches, but the core idea is the same: the site must open up the URL and render the page in order to grab the screenshot. So what kind of measures would one expect them to take to thwart viruses and malicious URLs?
The biggest threat to your application would be client script executing in your browser control (i.e. JavaScript and client-side VBScript). It appears it is not possible to disable JavaScript programmatically in the WebBrowser object:
VB.NET WebBrowser disable javascript
Disable javascript in
WinForms WebBrowser control?
Stripping <script> tags in the first question's first answer is not the way to go for security, as there are so many other ways script can get inserted.
Changing window.alert in the second answer won't work as it needs the page to load fully first, and it is possible for script to execute before then. Also, this would only stop the alert function and not prevent script code in any other way.
Changing the registry settings as suggested in this answer may be the way to go, but this appears to be the same as changing Internet Explorer settings to high security for the internet zone (or selecting custom and disabling Active Scripting). If you are always in control of the machine where your app is loaded from, then manually disabling scripting in Internet Explorer options could be a viable solution.
Most client-side internet threats such as drive-by downloads involve script in some way, so this approach will go a long way in protecting your app.
However, there are other exploits such as the Windows Metafile vulnerability that can harm a client machine.
Viewing a website in a web browser that automatically opens WMF files, in which case any potential malicious code may be automatically downloaded and opened. Internet Explorer, the default Web browser for all versions of Microsoft Windows since 1996, does this.
However, making sure your machines are patched with the latest Windows Updates will secure you against threats like these. This will leave zero-day attacks against Internet Explorer or the WebBrowser object, which you will not be able to do much about. I would suggest running your app on an isolated machine (or VM) which would then upload the screenshot to another server (e.g. via the web) which would help mitigate threats in this scenario.
i wish to interact with my browser window may be IE great if it works on Firefox too, using C#.
I want to make a software which can fill the entries in a webform automatically. In old times there was gator now is roboform, where it can fill in the values automatically.
I actually have users who are comfortable working on an old windows forms application, so i want to make a solution where they can still enter the data in their windows application and it actually fills in the entries at the web form and acts as if the request had generated from the browser itself.
I know i can merge both the databases, since it is a legacy application re writing the database for windows app is a trouble..
Any suggestion?
WatiN is designed to make testing web applications easy from .NET, and it sounds like it could be handy for what you want to do:
Following is the Hello world example
of web test automation; searching
Google.
[Test] public void
SearchForWatiNOnGoogle() { using (IE
ie = new IE("http://www.google.com"))
{
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(ie.ContainsText("WatiN"));
} }
WatiN Feature List
Automates all major HTML elements
Find elements by multiple attributes
Supports AJAX website testing
Supports frames (cross domain) and iframes
Supports popup dialogs like alert, confirm, login etc..
Supports HTML dialogs (modal and modeless)
Works with Internet Explorer 6, 7, 8 and FireFox 2 and 3
It's billed as a testing application, but Selenium RC can be used to fill in forms and is fairly easy to setup. You could also check out WatiN. Don't know anything about what security issues you might see though.
You might also want to check out Selenium which is a web application testing framework that you can programmitically interact the web UI.
If you use fiddler you may be able to see what the browser sends back to the server, and so you could write C# code to generate the same kind of HTTP request.
If the interaction is very complex (it often is with modern webapps), you could instead automate the browser, as you suggested.
I've had some success automating IE by using the InternetExplorer.Application object. It basically launches a copy of IE and lets you control it from code. I wrote a script this way a few years ago to search for cheap train ticket reservations for me on the Virgin Trains website.
The problem was that with some IE installs, it would sometimes stop to give security warnings that I couldn't skip automatically. There didn't seem to be a pattern to this.
If your users are simply using the application via a WindForms application, then is there any particular reason why you have to manipulate the user interface of an existing web browser, such as Internet Explorer, rather than just making the necessary HTTP requests yourself in your WinForms application? You can use the WebRequest class by setting the Method property to "POST" and writing the field data to the Stream, which you can get using the httpRequest.GetRequestStream() method.