I have a class of library code which uses a System.Windows.Forms.WebBrowser control to load a web page then extract information from the DOM tree, etc... I've found this to be the only reliable way to be able to parse information loaded onto a page with javascript, etc...
This worked fine when testing with console app, but when I call the same method from an asp.net page (ASP.NET MVC Controller) it crashes with a "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."... i've tried adding [STAThread] to my method but that doesn't seem to help!
Any ideas?
Thanks
Update:
I need to allow a page to complete load and execute all its javascript, then search it for text that matches a template I have already stored... Don't think HtmlAgilityPack will work? Does WatIn run the javascript?
Save yourself a lot of headache and use WatiN instead of a WebBrowser control.
See this question: Best method for Website Automation?
I believe it is impossible to get IIS to run in single thread apartment model (STA). Instead of using the WebBrowser control, you could take a look at the HTML Agility Pack. It will let your read in a web page and poke around the DOM. The WebBrowser control uses lots of computer resources that you won't have to worry about it make the switch.
Everything in the System.Windows is intended for desktop, not web stuff. I agree with Jake Pearson that you should use something else. But if you want you can try forcing the page into STA mode using this directive:
<%#Page AspCompat="true"%>
http://msdn.microsoft.com/en-us/library/zwk9h2kb.aspx
Related
I am developing web browser in C# windows form application using WebBrowser component.And also a website in ASP.NET using C#.Both are different projects.How can i use value of Label from windows form application and use the same value in ASP.NET website?
Help me !
Thanks in advance !!
Did you know you can write code to automate browsers, such as IE or Firefox? I mean, write code to launch a browser window, load pages at will, easily access particular parts of web pages, simulate user input (textboxes, controls, buttons, etc). It's commonly used to automate testing of web pages, and what-not.
There are multiple libraries out there, but I personally tried WatIn: http://watin.org/. Open source (free), .net component, easy to use, ton of examples.
I'm suggesting this as an alternative - if it fits your model. I know this doesn't answer your question; sorry.
There is a website that was created using ColdFusion (not sure if this matters or not). I need to interact with this web site. The main things I need to do are navigate to different pages and click buttons.
I have come up with two ideas on how to do this. The first is to use the WebBrowser control. With this, I could certainly navigate pages, and click buttons (According to This).
The other way is to interact with the html directly. Not sure exactly how to do this, but I am assuming I could click buttons or use HTML requests to interact with the page.
Does anyone have a recommendation on which way is better? Is there a better way that I haven't thought of?
I'd use Html AgilityPack to parse the html and then do POSTs and GETs appropriately with HttpWebRequest.
While it may be possible to use the WebBrowser control to simulate clicks and navigation you get more control with Html AgilityPack and HttpWebRequest regarding what gets sent
Did you consider Selenium? The WebDriver API is quite good, and permits a lot of things in terms of Website automation.
why not submit directly the url? that's what the button click will do.
using WebRequest.Create you can submit directly to the url. no need to load, parse and "click" the button.
HtmlAguilityPack is useful for pulling the web elements and finding tags easily. If you need to remotely "steer" a web session, though, I prefer to use WatiN. It bills itself as a web unit testing framework, but it's very useful anytime you need to fake a browser section. Further, it can remote control different browsers well enough for most tasks you'll need (like finding a button and pushing it, or a text field and filling in text if you need a login).
I am currently playing a little bit around with the development environment. At the moment I am writing a small app, which adresses a Webbrowser-Control.
Therefore I was looking for an opportunity, to show the URL-bar and maybe manipulating the URL via input of the user. Is this anyway possible, or not implemented for the webbrowser control?
Thanks in advance!
The WebBrowser control itself doesn't have a URL bar. It is simply a control that has the ability to display HTML and run Javascript. The easiest way way to simulate one would be to create a textbox. You can then use its Navigate method to load the webpage:
myWebBrowserControl.Navigate(myTextboxUrl.Text);
Alternatively, you can use the WebBrowserTask, but your app loses all control of the user's activities within this task.
I think it'd be easier to just launch the web browser task directly. Customizing the URL bar would probably require rolling your own.
Here's a related question about opening the browser: Open webbrowser with specific url in WP7
I need to refresh browser via c# code and google keeps silence. Any help would be very appreciated.
The awful, round about way to do it is to attach WatiN to the process (find the HWND, attach that way, most likely) and call Browser.Refresh().
So, you can't do it with C#, because C# isn't used on the browser. You can do it through JavaScript though.
<!-- Codes by Quackit.com -->
Refresh this page
http://www.quackit.com/javascript/javascript_refresh_page.cfm
or: Meta refresh Tag
If you wanted to split hairs, you could technically, do it in silverlight, but that really just ends up calling JavaScript:
Can Silverlight initiate Page Refreshes?
Response.Redirect("the_page_you_want_to_refresh.aspx");
Consult your browser's documentation for exposed APIs that may allow you to refresh the browser page from a different application, then perform whatever appropriate interop is required to refresh the page.
Is it possible to build a silverlight desktop web browser?
(just for fun, to see if it works etc) :)
I don't see why not.
With Silverlight 4 you can build a full trust application utilising a Web Browser Control (Source).
There's more details on the WebBrowser Class on the MSDN.
Hosts HTML content within the Silverlight plug-in.
Yes you can. Using WebBrowser control is not fun and defeats the purpose of writing a browser. Basically you would have to load raw html and parse it into DOM-like data structure. Next you'd have to use Silverlight visual elements and layouts to render html page as close as possible to the way real browsers do it.
You're likely to run into Silverlight limitations in process and creating browser compatible with CSS3 would require huge amount of work, so while in theory it should be possible I'd just stick to that knowledge and not try to actually do it.