2

I'm working on a program that automatically re-posts Craigslist posts in a batch form.

I'm executing a method called arrayTest() that contains a line webBrowser1.Navigate(urls[i]);. This will get a URL from an Array and have the webBrowser1 open it. After that is executed, the clickRepost(); method is called. This method simulates a button click. But the method executes before the webBrowser1 gets to finish opening the URL.

What i need help on is telling the program to wait for the webBrowser1 to finish opening the URL before moving on to the next method.

What i have tried so far:

Task t = Task.Run(() => {
    webBrowser1.Navigate(urls[i]);
})
t.wait();
clickRepost();

This still causes for clickRepost() to not let webBrowser1 finish loading the URL. I also tried to add System.Threading.Thread.Sleep(5000) in between calling methods but as it does so in Java, it only shuts the program for x-Seconds.

This is a segment of my program, really the only ones I'm having trouble with.

private void arrayTest()
    {
        String[] urls = CLLinks.Text.Split('\n');
        for (int i = 0; i < urls.Length; i++)
        {
            log.AppendText("Opening Url...\n");
            webBrowser1.Navigate(urls[i]);
            log.AppendText("Reposting...\n");
            clickRepost();
            log.AppendText("Checking Completion...");
            webBrowser1.Navigate(urls[i]);
            completionCheck();
        }
    }

    private void CLRenBtn_Click(object sender, EventArgs e)
    {
        arrayTest();
    }

    private void completionCheck()
    {
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");

        foreach (HtmlElement item in col)
        {
            if (!(item.GetAttribute("value") == "Renew this Posting"))
            {
                log.AppendText("Renew Posting does not exist");
            }
            else
            { }
        }
    }

    private void clickRepost()
    {
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");

        foreach (HtmlElement item in col)
        {
            if (item.GetAttribute("value") == "Renew this Posting")
            {
                item.InvokeMember("click");
            }
            else
            {}
        }
    }

CLRenBtn_Click Is the button i press on the form to start the Re-posting. arrayTest() will be changed to a more relevant name. At the time i was just testing whether this would work. And im planning on merging clickRepost() and completionCheck() since they do the same thing.

Many thanks in advance!

xR34P3Rx
  • 395
  • 9
  • 28

1 Answers1

0

Found an answer! C# how to wait for a webpage to finish loading before continuing

Worked good for me, but now my little problem is that, when i start the reposting. it skips the first url in the array and posts the ones that follow.

Community
  • 1
  • 1
xR34P3Rx
  • 395
  • 9
  • 28