I have a C# winforms application that opens a pdf file in a webbrowser control. It opens to whatever page I want just fine however if I want to change page (go to a bookmark) the webbrowser stops working. I found this article which explains "Basically Webbrowser.Navigate(url) ONLY fires if the url changes. If it
doesn't change it uses a cached version of the web page." however I am calling navigate with a Uri, not a string url like this:
webBrowser.Navigate(new Uri(url));
My question is simply: how can I navigate to another page in the same pdf file, after I have opened the file in the web browser?
Of course I figure out the problem 2 mins after posting a question. I'll post my solution in hopes it helps someone else anyway;
So to make this work I used this workaround:
webBrowser.AllowNavigation = true;
webBrowser.Hide();
webBrowser.Navigate("about:blank");
await Task.Delay(1000);
webBrowser.Navigate(new Uri(url));
webBrowser.Show();
This code may be useful to you.
public static void GetAllText(WebBrowser webBrowser,int toPageNum)
{
webBrowser.Focus();
for(int i = 0; i < toPageNum; i++)
SendKeys.Send("{PGDN}");
}
Related
So I have a website which I load into my form's webbrowser control. After loading the document, I retrive the webbrowser.documenttext . I am looking here to parse specific table. But I am not finding the table in here but I see that it is being dispayed in the form browser.
I tell you that this specific table is being loaded/appended to doc by already loaded javascript code.
When I right click and select the "View Source" , it pops the document with correct html.
My question is how can I get the same document referenced by ViewSource or is there any way to get the document what is being rendered on form?
Instead of using Webbrowser Control use HtmlAgilityPackage to parse data based on your need.
var html = new HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.asp.net"));
var root = html.DocumentNode;
var commonPosts = root.Descendants().Where(n => n.GetAttributeValue("class",
"").Equals("common-post"));
Similar Existing Question
The above issue was very similar to my issue and after going thorough the answer I learnt that I somehow need to wait and poll the webbrowser to get the dynamic content.
I did not really implement the code provided in the answer but I changed my documentCompleted event as async and provided a await task delay of 5s
private async void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
await Task.Delay(5000);
var html= wb.Document.GetElementsByTagName("HTML")[0].OuterHtml;
}
Now I get the dynamic result. Thanks, I am feeling now.
I am facing this issue, working on Active reports 9. Every thing is fine as per our application we generate the report and in UI user will be viewing in a c# Web browser control.
Now the issue am facing is when client(user) clicks on the link present in pdf i.e. on Web Browser control. With in the same window the link is opening. They want the link to open in new window.
The problem q=am facing is if its Html control i would have used target="_blank" property but not , and its a windows application i cant even use Java script. I just gone through the properties of Picture control used in Report, theres only Hyperlink property which states in pdf it converts it to href or a tag.
Need some assistance as soon as possible is that possible to do in web browser control or should change any properties for picture control in Code behind.
Hope this help you. It worked for me.
Add the Navigating event on your webBrowser control. This will open the link in a new Browser window. In my case Google Chrome.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
try
{
if (!(e.Url.ToString().ToLower().Contains("file") || e.Url.ToString().ToLower().Contains("pdf")))
{
e.Cancel = true;
//Open Link
Process.Start(e.Url.ToString());
}
}
catch (Exception err)
{
//Handle Exception
}
}
I'm coding a windows phone 7.1 app, and when the user clicks a specific button, a youtube video would be played using a MediaPlayerLauncher.
Problem is, MediaPlayerLauncher can't play the video just by giving it the video's url; the video's link itself is in the page's html. Now, I managed to pull out that html by using a WebClient() to download the html and extract the video's link from it, by attaching this event for 'client', my WebClient:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
pageHtml = e.Result;
stringBuilder = new StringBuilder(pageHtml);
if (pageHtml != null)
{
if (pageHtml.Contains("<html"))
{
if (pageHtml.Contains("<script"))
{
stringBuilder.Replace("</script>", string.Format("{0}{1}</script>", NOTIFY_SCRIPT, Environment.NewLine));
}
else if (pageHtml.Contains("<head"))
{
stringBuilder.Replace("</head>", string.Format("{0}{1}</head>", NOTIFY_SCRIPT, Environment.NewLine));
}
else
{
stringBuilder.Replace("</html>", string.Format("{0}{1}</html>", NOTIFY_SCRIPT, Environment.NewLine));
}
}
else
{
//Just skip it or display an error message or whatever
}
rssBrowser.NavigateToString(stringBuilder.ToString());
}
}
Basically, I add a script, 'NOTIFY_SCRIPT', which detects the presence of the youtube video(if you want more details about this, the video's link is basically in an tag, so I just get all the tags, find the one with the link, and get it's contents (the link)).
But still, this just doesn't work. I tried putting up a WebBrowser and making it navigate and triggering the event every time the WebBrowser navigates, in order to make sure that it's navigating to the correct page. But sometimes, it doesn't navigate properly; it gets stuck on an intermediate page or goes to the original youtube page. So, I decided to take a look at the incoming html. For some reason, the incoming html is missing youtube's script. I checked the script on the youtube page using my browser (I went to the mobile web page and 'inspected the element'), and it has a script there, but when the WebClient gets the html the script is missing.
So maybe that's the problem? Does anyone know how to solve this problem, or maybe you have already done something like that in a different way?
Sorry for the long question, and thanks!
You may be better of using the WebBrowserTask or the API to get the URL.
I'm writing an app for Windows Phone 7/Silverlight. When the app is either tombstoned and reactivated while on the app page containing the WebBrowser control (I've saved the Uri in app state) or that same app page is navigated to by NavigationService.GoBack() or the phone back button, it seems that as long as the control still has the webbrowser.source value, it should then render just fine, but this is not the case. Unless I use the Navigate() method, it shows a white/blank screen, no matter what I try. Unfortunately, using the Navigate() method causes the web content to download again, unnecessarily. It's especially frustrating when only a GoBack() is used to get back to the application page with the WebBrowser control, which is quite frequent in my app.
private void OnWebBrowserLoaded(object sender, RoutedEventArgs e)
{
//webBrowser1.Source = CurrentUri; //does not work, results in white/blank browser page
webBrowser1.Navigate(CurrentUri); //works, but page has to reload from web, bad UX
}
Any suggestions on a way around this problem? I've also tried putting this same code in the page loaded handler. It behaves in the same poor manner.
I've also tried saving off the HTML (SaveToString) and reloading it from app state (NavigateToString), but the web page does not render completely for some reason, even though the HTML appears fine. Also, I'd like to have access to the Host and Uri properties. I could probably work around that, if I could get the HTML to render OK from NavigateToString.
Thanks,
Jay
You should use browsertask:
using Microsoft.Phone.Tasks;
WebBrowserTask browse = new WebBrowserTask();
browse.Uri = new Uri(URL, UriKind.RelativeOrAbsolute);
//new Uri(URL,UriKind.RelativeOrAbsolute);
browse.Show();
This should solve your issue.
URL will be the URL of the page you want to visit.
i want to know how i can open a browser to a specific web page and then fill out some of the content of the boxes on that page.
My idea is for someone to be able to order a particular item from our internal ordering system. The barcodes for these items are what will populate the fields on the page i want to open.
I no i can open a new instance of ie using Process.Start("IEXPLORE.EXE", url); howver how do i get a handle on that exact ie instance window so i can begin to add the required data to the fields?
Is this even possible?
Thanks very much
WatiN should help with this. I've generally used it for acceptance testing of web apps, but the principle is the same. Open a browser instance, reference stuff in the DOM, manipulate form elements, etc.
In addition to WatiN (as was suggested in another answer), you might consider a load testing package like Web Performance Load Tester. They have a free version that lets you run up to 10 virtual users at a time, which will perform scripted actions.
Another option would be to use a standard WebBrowser object to load your website. The WebBrowser object allows you to access and alter certain web parts. Below is sample code that automatically searches Bing:
private void LoadPage()
{
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("http://www.bing.com");
//Wait for document to load...
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
//Set the text of the search input
HtmlElement txtTextField = webBrowser1.Document.GetElementById("sb_form_q");
txtTextField.InnerText = "My test text";
//Perform a click on the search button
HtmlElement btnSubmit = webBrowser1.Document.GetElementById("sb_form_go");
btnSubmit.InvokeMember("click");
}