I am having trouble displaying html onto a web browser. I am using the method "Deployment.Current.Dispatcher.BeginInvoke" but it wont displaying anything. What I am trying to do is getting html from a server as a string and displaying in my web browser. I can display messagebox but i cant send the string that contains html onto my web browser.
Here is the snippet of code that is giving me trouble:
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
WebBrowser webBrowser1 = new WebBrowser();
//MessageBox.Show(responseString);
webBrowser1.NavigateToString(responseString);
});
I think the reason for this error is because you're creating the WebBrowser-instance inside your callback code - which you shouldn't do anyways (because how are you going to display it on the screen? If you are trying to go from your application into the main WebBrowser-app (IE), you should use WebBrowserTask instead.
So either:
you are trying to show a WebBrowser inside your application view (hybrid style). If so, you should call .NavigateToString(string html) on the WebBrowser you're displaying instead of creating another instance.
you are trying to show downloaded HTML in the main browser app on your phone (Internet Explorer). This can't be done by handing over the HTML directly, you would have to send it the URI-object where the browser can find it itself.
Or of course, the problem is somewhere else in your code. This is the best I could answer with the information and code you have provided.
Related
Hey guys I have a program that still uses the old WebBrowser Xaml control. Now I wanted to exchange this on WebView2 here I have problems. Currently, the body of an email is pulled from a database as html code and written to a variable, which is linked to the WebBrowser control. Only WebView2 wants a URL to be passed in the source function. How can I pass a variable containing HTML code instead of a link to a web page? Or are there other alternatives for this?
I would suggest you try NavigateToString
await webView21.EnsureCoreWebView2Async();
webView21.NavigateToString(anEmailHTMLVariable);
Is it possible to embed only a specific portion inside of a page(i.e. div) on the WebView control in UWP?
The webview doesn't include an address bar and so will only show pages that you load or can be navigated to from those pages. If you can control what is loaded you can show only the pages you wish.
If you don't have control over the HTML of the site, you could prevent access to pages you don't want by handling the NavigationStarting event and canceling the navigation if it's to a page you don't want displaying.
No, assuming by a "portion" of a website you mean a portion within a page. However as Matt said, if you only want to show a certain page, you can of course navigate to the relevant web page and then control what navigation links you allow through his method.
If you did indeed mean only a portion within a page, the options aren't attractive. Theoretically you could pull down the html of a site with an Http request and then parse it to filter out what you didn't want, store it locally, and then load that local page in the webview. However I think it would add significant overhead, could create conflicts with javascript or whatever else the site might be using, among other things. But hey maybe if it's a very targeted use-case you might be able to play with it.
If in your scenario though you also control said website the app is accessing, then I'd just enable specific URLs to offer up the desired versions of your site when the app requests 'em.
Note 100% sure, but i can give a hit to embed external part of a any html content to a website opened in webview control.
var result = await this.webView.InvokeScriptAsync("eval", new[] { "document.documentElement.outerHTML;" });
string result = await this.webView.InvokeScriptAsync("eval", new [] {"document.getElementById('tablePrint').innerHTML = myTable;" });
Add HTML content through java script.
Hope that will help
I am attempting to get the resulting web page content so I can extract the display text. I have attempted the code below but it gets me the source html and not the resulting html.
string urlPath = "http://www.cbsnews.com/news/jamar-clark-protests-follow-decision-not-to-file-charges-in-minneapolis-police-shooting/";
WebClient client = new WebClient();
string str = client.DownloadString(urlPath);
Compare the text in the str variable with the html in the Developer Tools in the Chrome browser and you will get different results.
Any recommendations will be appreciated.
I'm assuming you mean that you want the article text. If so you will need to follow a different course of action. The page you refer to is loaded with client script that injects loads of content into the base HTML document. This is done by executing the client-side script. You will need to parse the DOM after the script is executed to get the content you're interested in.
As others have pointed out, an actual web browser will parse the downloaded HTML and execute javascript against it, potentially altering its content. While you could try to do that parsing yourself, the easiest route is to ask a real web browser to do it for you and then grab the results.
The easiest solution specifically in C# would be to use the WebBrowser Control from Windows Forms, which essentially exposes IE to your program, allowing you to control it. Use the Navigate method to load the URL in question, then use the Document property to navigate the DOM. You can, at that point, get the outerHTML to get the final content of the DOM as HTML.
If you're not writing a Windows program and are interested more in headless operation, have a look at PhantomJS. It's a headless Webkit browser that is scriptable from javascript and would give you similar capability, although not in C#.
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.
I have a web page which displays some report. When I click on the link for excel, it puts all the data that is displayed on the screen into a CSV file and opens a File Download window which lets me either Open, Save or Cancel.
I want to download this file to a specified location programatically without the user seeing the File Download window. I am using a Windows Application. I dont have the code for the website displaying this report hence dont have the dataset. Also, I looked into the HTML that gets generated by doing a view source on the page but I can't really scrape through it to get the data.
Hence I need to know how to download an excel file from a given website to a location on my computer.
Thanks in advance
Rita
Well you'll need to find the appropriate link or post action. You can then use something like:
string url = "http://whatever/.../foo.xsl";
string target = #"c:/excel documents/foo.xsl";
WebClient client = new WebClient();
client.DownloadFile(url, target);
It gets a bit more complicated if you need to authenticate etc though.
I assume that you're showing the web page within a WebBrowser control. You can handle the WebBrowser.Navigating Event and inspect the value of the WebBrowserNavigatingEventArgs URL property to see if string.EndsWith(".csv", CurrentCultureIgnorecase) (or perhaps a more specific test).
If you detect that the link that has been clicked is for a file you wish to download then you can call e.Cancel() = True on the WebBrowserEventArgs instance to stop the browser from proceeding with its normal behavior of opening the File Download dialog. Then you can use .NET's HttpWebRequest and HttpWebResponse classes to manually download the file.
Here's a sample of downloading using these classes
Actually I am doing a screen scraping, so I am not displaying the web page.
What I have done is gone to the URL where the report is displayed on the screen. There is a hyperlink for 'Excel'. I make this hyperlink click from my program itself. On clicking this link, it opens the File Download dialog box.
I have written, the WebBrowser.Navigating and Navigated events, but the control does not come here. Any ideas
1 more thing, the site that I am accessing is java website.