How to add a stylesheet into a WinForm WebBrowser in C#? - c#

I am wanting to manipulate a page being loaded in a WebBrowser by adding a stylesheet on the DocumentComplete event. I'd like to do it without using an external file, if possible.
Most of the solutions I've seen involve deprecated methods or libraries such as mshtml. The MSDN says that the accepted method is to use HtmlElement myelement = mywebbrowser.Document.CreateElement(
"style"), but nothing else. Manipulation of such seems to be problematic - things like trying to set InnerHtml throws an error at runtime (ie, System.NotSupportedException: Property is not supported on this type of HtmlElement).
Given all that, what is the best/proper way to add a big chunk of CSS to a page loaded in a WebBrowser?

Related

Web automation using C# WebBrowser

I'm in the very early stages of attempting to automate data entry and collection from a website. I have a 16,000 line CSV file. For each line, I'd like to enter data from that line into a textarea on a webpage. The webpage can then perform some calculations with that data and spit out an answer that I'd collect. Specifically, on the webpage http://www.mirbase.org/search.shtml, I'd like to enter a sequence in the sequence text box at the bottom, press the "Search miRNAs" button and then collect results on the next page.
My plan as of right now is to use a C# WebBrowser. My understanding is that I can access the individual elements in the HtmlDocument either by id, name or coordinate. The last option is not ideal, because if I distribute this program to other people I can't be sure they'd be using at the same coordinates. As for the other 2 options, the textarea has a name, but it's the same as the form name, so I don't know how to access it. The button I'd like to click has neither a name nor an id.
Does anyone have any ideas as to how to access the elements I need? I am by no means set on this method, so if there's an easier/better way I'm certainly open to suggestions.
The WebBrowser class is not designed for this, hence why you are coming up with your problems.
You need to look into a tool that is designed for web automation.
Since you are using C#, Selenium has a wonderful set of C# bindings, and it can solve your problems because you'll be to use different locators (locating an element by a CSS selector or XPath specifically).
http://docs.seleniumhq.org/
Check mshtml - Mshtml on msdn
You can use it with the WebBrowser object.
Add Microsoft.mshtml reference to your project and the using mshtml declaration in your class.
Using mshtml you can easily set and get elements properties.

Using the res:// protocol with System.Windows.Forms.WebBrowser

I'm trying to use a resource (html file) located in a dll. With WinForms WebBrowser, when I navigate to the file, nothing happens, while with included AxSHDocVw.dll and SHDocVw.dll and AxWebBrowser, it works. Is the WinForms WebBrowser control somehow restricted or something? Can I make it to run res://?
See my post here: https://stackoverflow.com/a/15672462/1413201.
The basic gist is there are two types of resources in code files. You need to include a C style resource script to use the res protocol. Navigation errors are probably turned off in the WebBrowser control and therefore you don't see an error.
You can use the res protocol with IE to test if the resource is actually in the file and a C style resource editor just to double double check.
What I would assume, is that for security reasons, WinForms' WebBrowser control doesn't handle res:// links. It would make it very easy for someone to access resources which are contained within your DLLs which you may not want accessed.
If you want to implement the functionality yourself, then I'd recommend looking at the Assembly class and its use. It shouldn't be hard to parse a res:/// into your DLL path, load the assembly, search for the given resource and return that for the WebBrowser control.

Calling ActionScript (Flash) method via mshtml from C#

I use the WebBrowser component from WPF. I load there a page from Internet and I access the Document property to call my scripts. Unfortunately calling scripts don't work as I need. When I am calling JavaScript methods which exist in JavaScript so it works great but when I need access some methods which are available through Flash so I have problems.
document.parentWindow.execScript("document.getElementById('swfObject').methodFromActionScript();", "JavaScript");
I get an exception in the browser Object doesn't support this property or method. In C# I get Exception from HRESULT: 0x80020101
But when I try launch this code in a regular browser as IE or Chrome by passing it into URL so the code is executed and I see results.
Maybe some trustmode issues or what else could deny access to Flash properties and methods?
BTW: The var allowScriptAccess is set to always.
Thank you for help.
Ok, I've found an issue. Before this execScript. I was doing some modification of DOM and moving elements and apparently this has broken DOM so I couldn't call swfObject because during moving it was somehow modified and lost its properties and methods which were exported via ActionScript.

Any way to associate a HtmlElement (.NET) to a JavaScript element?

I'm trying to make an extended version of a WebBrowser with stuff like highlighting text and getting properties or attributes of elements for a Web Scraper. WebBrowser functions doesn't help much at all, so if I could just find a way from HtmlElement to a JavaScript element (like the one returned by document.getElementById), and back, and then add JavaScript functions to the HTML from my application, it would make the job a lot easier. Right now I'm messing with the HTML of the code programmatically from C# and it's very messy. I was thinking about setting some unique Id to each HTML element from my program and then call the JavaScript document.getElementById to retrieve it. But that won't work, they might already have an Id assigned and I will mess up their HTML code. I don't know if I can give them some made up attribute like my_very_own_that_i_hope_no_web_page_on_the_world_ever_uses_attribute and then figure out if there is some JavaScript function getElementByWhateveAttributeIWant but I'm not sure if this would work. I read something about expansion or extended attributes on the DOM documentation in msdn but I'm not sure what that is about. Maybe some of you guys have a better way.
It would be much easier to use some rendering engine like trident to get the data from html document. Here is the Link for trident/MSHTML. you can do google and can have samples in c#
This is not nearly as hard as you imagine. You don't have to modify the document at all.
Once the WebBrowser has loaded a page, it's kept internally as a tree with the document node at the root. This node is available to your program, and you can find any element you want (or just enumerate them all) by walking the tree.
If you can give a concrete example, I can supply some code.

How to Use a java Applet through c#?

I would like to use a java applet which is on an html file as ..
This java applet contains a textbox, and i want to access it as whenever I click a button in my c# app a certain txt appears there...
I have already embedded it with the use of webBrowser..
Any sample code, or suggestion?!..
Note: development under visual studio 2008 and I have the java applet source code..
The WebBrowser control isn't going to give you direct access to methods on a Java applet, AFAIK (you might be able to fudge your way through because I can't imagine that there isn't ActiveX involved, but I don't think that's the best answer).
Rather, what I would do is have a function in JavaScript that accesses the value of the textbox from the applet (assuming that the control in the applet exposes it publicly). It would assign the value to a property on the extern object exposed by the window object in JavaScript.
Then, you would create your own class which you would assign to the ObjectForScripting property on the WebBrowser control which exposes that property.
Finally, when you click your button, you would call the script (you can assign the function to your same object, if you want) which will assign the value, then get the value from the instance that you set to the ObjectForScripting property on the WebBrowser control.
There are other ways you can do this as well, you could have the JavaScript method write the text to a hidden element, then browse the DOM model to get the value, etc. etc.
In the end, whatever solution you have will more than likely involve a combination of DOM traversal and setting the ObjectForScripting property, as well as JavaScript in the HTML hosting the applet.
Another approach you can take is to create a UserControl or WebPart, which writes the HTML you need in the Render method. Here is a good link on the Render method (http://msdn.microsoft.com/en-us/library/system.web.ui.control.render.aspx).
We took this approach at one project I was on, where the <object> and <param> tags were created using custom configuration (Web.config and configuration sections) to render the proper HTML for a 3rd-party Applet.

Categories