4

As per requirement I need to scrape the content in a web page. For implementing this I am created a windows application and added a web browser control to the main form. So that I can view the scrapping process. I am able to login to the webpage and navigating to the desired web page. Also I am able to double click the grid cell programmatically. But the current issue is I am getting an alert message when programmatically double clicking the grid cell and if the desired data is not availableenter image description here

So naturally scraping process is interrupting and manually we need to to click “OK” button to continue the scraping process. How can I avoid the arrival of alert messages while scraping?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ranish
  • 877
  • 2
  • 10
  • 30
  • possible duplicate: http://stackoverflow.com/questions/77659/blocking-dialogs-in-net-webbrowser-control – Giedrius May 30 '13 at 05:11
  • I already used alert overloading.But unfortunatly it is not worked – Ranish May 30 '13 at 05:21
  • So may be it is coming from some kind of plugin? Or is it javascript error, in such case you can disable them with `webBrowser.ScriptErrorsSuppressed = true`. Another question is, do you really need web browser control for scrapping, as this approach is way slower than doing requests manually? – Giedrius May 30 '13 at 05:54
  • Yes I need web browser control for scrapping, since they want to view the scrapping process – Ranish May 30 '13 at 06:45

1 Answers1

5

To programmatically click alert box we could use Windows API FindWindow,FindWindowEx and SendMessage, the idea is when a alert box appears, the form which host WebBrowser control will lose its focus, at this time, we'll be able to use FindWindow to retrieve the window handle of the alert box, finally we could send a click message to the "Ok" button to click it.

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        IntPtr hwnd = FindWindow(null, "Message from webpage");
        hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
        uint message = 0xf5;
        SendMessage(hwnd, message, IntPtr.Zero, IntPtr.Zero);
    }
Palak.Maheria
  • 1,497
  • 2
  • 15
  • 32