views:

3110

answers:

5

Ok, I have a very frustrating problem. I am parsing a webpage and need to have it execute the javascript in order to get the information I want.

Form f = new Form();
WebBrowser w = new WebBrowser();
w.Navigate(url);
f.Controls.Add(w);
f.ShowDialog();
HtmlElementCollection hc = w.Document.GetElementsByTagName("button");

This works and I'm able to get the button elements fine, but then I get a popup everytime I run this. Very annoying. The popup is javascript based and I need to run Javascript to get the button element info. Here is the script for the popup.

<script>
var evilPopup = (getCookieVar("markOpen","PromoPop") == 1);
if (evilPopup != 1) 
{
    PromoPop = window.open('/browse/info.aspx?cid=36193','Advertisement', 'width=365,height=262,screenX=100,screenY=100');

if (PromoPop) 
    {
       PromoPop.blur();
       window.focus();
       setCookieVar("markOpen","PromoPop","1");    
    }
}
</script>

I tried in vane to possibly add a cookie to the Forms.Webbrowser control but got frustrated and gave up. I tried setting the NoAllowNavagate property and everything else to no avail.

Can anyone help? Also, there a way to get the DomDocument info from the Console.App without having to open a form?

Thanks

A: 

Can you inject Javascript into the document? You could add this:

window.open = function() { return false; }
Greg
I am really lame when it comes to Javascript. I didn't even realize your could inject Javascript. If that's the case, I might not even have to go the route I am going now. Thanks for your comment, I'll look into that.
+2  A: 

The WebBrowser component has NewWindow event with CancelEventArgs. So just add a handler similar to:

void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
    e.Cancel = true;
}

When the javascript tries to open a popup window the event gets triggered and cancels it.

Mikko Rantanen
Wow, I am going to try this out right now. This looks very promising and has the "duh" factor to it. Okay I just tested it out 15 times and it works great! A special place in my heart is now reserved for you kind stranger.
A: 

A quick and dirty solution would be to use WebClient to download the html to a temporary file, replace the ad script with string.Empty and load the file in the control.

I tried this approach I think, I tried to load the html in Stream afterwards but didn't have any luck. I don't know I have to study WebClient further, I tried to do what you mentioned using HttpWebRequest/WebResponse. Thanks
A: 

hii, i m try to develop an application. i m using .net framework.and mshtml library. i have two problems. please help me. first of all

 mshtml.IHTMLDocument2 doc = null;
            try
            {
                doc = (mshtml.IHTMLDocument2)((mshtml.HTMLDocumentClass)wb1.Document.Window.Frames["content"].Document.Forms["SForms"].Document.DomDocument);
            }

sometimes i get an error that is An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

why does it happen? what should i do?

second one is, i try to open an url. when i open webpage it directs me another page, but it opens in another webbrowser. i want to open it in my webbrowser1. how can i that? i tried to webBrowser1.Navigate("https://www.yazilimisleri.com",false); but still it opens in another webbrowser. thanks a lot.

A: 

hi you should try SHDocVw.dll to automaticli catch new window.

private SHDocVw.WebBrowser_V1 Web_V1;

// write it on form load event

        Web_V1 = (SHDocVw.WebBrowser_V1)this.webBrowser1.ActiveXInstance;

        Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(Web_V1_NewWindow);


    private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        Processed = true; //Stop event from being processed

        //Code to open in same window
        //this.webBrowser1.Navigate(URL);

        //Code to open in new window instead of same window
        frmEBeyanname Popup = new frmEBeyanname();
        Popup.webBrowser1.Navigate(URL);
        Popup.Show();
    }

bye

ismail