开发者

How to click on multiple links on multiple pages in webBrowser

Iam Unable to do this from past one week. I want to click on multiple links n multiple web pages using webBrowser in C# Following is the code please help me in this regard.

public void DoDelete()
{
    int count = 0;

    if (corruptList.Count > 0)
    {
        foreach (string listItem in corruptList)
        {
            var th = new Thread(() =>
            {
                try
                {
                    WebBrowser webBrowser = new WebBrowser();
                    webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBroswer_DocumentCompleted);
                    webBrowser.Navigate(listItem);
                    Thread.Sleep(100);
                    webBrowser.Dispose();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                this.Invoke(new MethodInvoker(delegate
                {
                    dataGridView_CorruptLinks.Rows[count].Cells[2].Value = "Deleted";
                }));
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
            Thread.Sleep(100);
        }
        count++;
    }
}


void webBroswer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    try
    {
        WebBrowser webBrowser = sender as WebBrowser;
        HtmlElementCollection ec = webBrowser.Document.GetElementsByTagName("a");
        foreach (HtmlElement item in ec)
        {
            if (item.InnerHtml == "Delete this invalid field")
            {
                item.InvokeMember("Click");
                break;
            }开发者_运维知识库
        }
    }
    catch (Exception exp)
    {
    }
}


Navigate is an asynchronous action and you're only giving it 1/10 of a second to complete before you call Dispose on the web browser object. Your navigation and clicks are probably taking longer than that to complete and so there is no web browser to act against... You're also "swallowing" all exceptions in the document complete handler. This is a very bad thing to do. You should at the very least be doing some debug logging there to help yourself diagnose the problem.

But, to keep the similar logic you should create a collection of web browsers at class level. Something like:

private List<WebBrowser> _myWebBrowsers;

Then add to this list in your loop but do not call Dispose. You should only dispose of the browser when you're done with it.

That should get you closer though there are a few other potential issues with your code. You're allocating a borser object and thread for every time through a loop. This could quickly become unwieldy. You should use a thread management mechanism to throttle this process.

Simplified class:

class WebRunner
{
    private List<string> _corruptList = new List<string>();
    private List<WebBrowser> _browsers = new List<WebBrowser>();

    public void Run()
    {
        _corruptList.Add("http://google.com");
        _corruptList.Add("http://yahoo.com");
        _corruptList.Add("http://bing.com");

        DoDelete();

        Console.ReadKey();
    }

    public void DoDelete()
    {
        if (_corruptList.Count < 1) return;

        int counter = 1;

        foreach (string listItem in _corruptList)
        {
            WebBrowser webBrowser = new WebBrowser();
            _browsers.Add(webBrowser);
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBroswer_DocumentCompleted);
            webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser_Navigated);
            webBrowser.Navigate(listItem);
            if (counter % 10 == 0) Thread.Sleep(3000); // let app catch up every so often
            counter++;
        }
    }

    void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        Console.WriteLine("NAVIGATED: " + e.Url);
    }

    void webBroswer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Console.WriteLine("COMPLETED!");
        try
        {
            WebBrowser webBrowser = sender as WebBrowser;

            HtmlDocument doc = webBrowser.Document;
            var button = doc.Body.Document.GetElementById("button");
            button.InvokeMember("Click");

            _browsers.Remove(webBrowser);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.StackTrace);
            MessageBox.Show(exp.Message);
        }
    }
}


You can access the WebBrowser document content using the following (you are missing body and need to type document to dynamic).

dynamic doc = browser.Document;
var button = doc.body.document.getElementById("button");
button.Click();


I found the solution very next day. Sorry for the late post by processing threads one by one by putting the statement after thread.sleep() if (th.ThreadState == ThreadState.Aborted || th.ThreadState == ThreadState.Stopped)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜