How to Use a java Applet through c#? - 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.

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.

Find URL Responses? Alternative To Default WebBrowser Control?

Hello guys I have an issue bugging me for the past few weeks.
What I'm trying to accomplish: I need a webbrowser control with the ability to change user agent (once at start) and referrer. But most important The ability to see the urls responses. What I mean by that for example if you navigate to a website you get back Images/Javascripts files/Dyanmic URLS in response I need access to those urls which some of them have dynamic variables (Regular Webbrowser Control will not show you those & you can't access it in any way beside using fiddler core).
I was able to do that with webbrowser + fiddlercore I can see and do what ever with those urls addresses. The problem was if you run few instances of this program (or sometimes once if the program has some automation to work with the url responses) It gets stuck or doesn't work. I tried fixing it and making it work but it's kind of a hacky solution that doesn't work right. I need a simple way to access those urls just as if you used httpwebrequest but as a webbrowser. Why I need it as a webbrowser? The way I work I need the execution of all the tracking pixels and scripts and images etc.. a normal webbrowser behaivor in httpwebrequest you can't just navigate and all the scripts will be execute as webbrowser, or can you?
Using the System.Windows.Forms.WebBrowser control in a WinForms app, set the webBrowser.URL property to the URL of the page you're interested in.
The webbrowser's DocumentCompleted event fires after the page has loaded. Any dynamically loaded JavaScript should be done by then. Hook the DocumentCompleted event and use the webbrowser.Document.Images to get a list of all image elements on the page. From those images you can get their SRC attributes which contains their URLs including any query parameters hanging off the end. You can use webbrowser.Document.Links to get a list of all hyperlinks on the page. For other HTML elements of interest, you can use GetElementsByTagName("foo") to fetch all elements with that tag name from the page, then dig into their attributes to pull out URL properties.
With webbrowser.Document you can get to any HTML element, whether it is statically or dynamically created.
What you can't get to through webbrower.Document is data that is loaded asynchronously using XMLHttpRequest(), because this data is not part of the browser Document Object Model. Web pages with scripted false buttons will be difficult to intercept.
However, if you know where the data is stored by the JavaScript executing on the page, you may be able to access it using webbrowser.Document.InvokeScript(). If the JavaScript on the page stores URLs in a mydata property of the window object, for example, you could try webbrowser.Document.InvokeScript("window.mydata") or some variation to retrieve the value of mydata into the C# app.

How to access text inside flash object

I've got to access text inside a flash object. The preferred method is via C#, but anything that works will do. The only requirement is that I'll need the resulting information to be transferred back to c# code.
Let's take, for example, this page.
With fireflashbug, you can easily access individual elements inside flash object and read/change html texts inside each of them.
To work, it requires Flash Debugger.
How can I do the same from my own code?
You can use AS3 ExternalInterface with or without JS. Check this thread too:
C# and Flash communication
To communicate with a flash object you use Javascript.
Here are some tutorial and examples:
http://www.adobe.com/devnet/flash/articles/external_interface.html
http://csl.sublevel3.org/howto/javascript_flash_callbacks/
http://www.permadi.com/tutorial/flashjscommand/
Now, once you have your data using javascript you can pass them to an input element and pass it to code behind if you wish for.
and many more on google

How can I get a handle or object reference to an HtmlElement from a WebBrowser control HtmlDocument?

Think "Firebug", but entirely from C#.
I have a WebBrowser control that I've built a DOM tree for in a TreeView. I'd like to be able to set a link between each DOM element in the TreeView and its matching HtmlElement in the WebBrowser's Document so that when the node in the tree is clicked, the matching element in the Document highlights.
But, of course, the only availability on the surface for element access is GetElementById(), GetElementFromPoint() and GetElementsByTagName(). And, of course, not all web pages have Id's or Names associated with them. And since in my app's user experience the user won't be clicking the WebBrowser, but the TreeView, I don't have access to a Point either.
I'm experimenting with various options I've found in the API now. But it would be great if anyone out there has experience in this area. I can't seem to find detail on the web anywhere.
Thanx ahead of time!
Personally, I would utilize a JavaScript library such as JQuery to perform such a task. This library is easy to use and plenty of examples/plug-ins available (http://jquery.com). Using JQuery allows you not only to use IDs but also grab them by CSS class, anchor type, etc. Essentially, anything you can pull from HTML/CSS you can pull with JQuery.
If you would like to handle a HtmlElement from the code-behind you essentially have to assign it an ID as well as specify the RUNAT attribute. For example:
<textarea id="bodyText" runat="server"></textarea>
Hopefully this helps in some way!
Have you tried the All property?

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.

Categories