等一下,用c#在几个网站上做点什么

本文关键字:网站 几个 什么 一下 | 更新日期: 2023-09-27 18:08:58

我想满载3个网站与一个浏览器(不是在同一时间),并做一些任务。

我的代码;

     string[] websites = 
     {
        "www.facebook.com", "www.bug.hr", "www.htmlgoodies.com"
     };
     string[] inputs = 
     {
        "first", "second", "third"
     };
     private void Form1_Load(object sender, EventArgs e) 
     {
        Browser1.Navigate(websites[0]);
     }
     private void Browser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     if (Browser1.Url.ToString().Contains(websites[0])) 
     {
        while (Browser1.ReadyState != WebBrowserReadyState.Complete) 
        {
            Application.DoEvents();
        }
        Browser1.Document.GetElementById("email").SetAttribute("value", inputs[0]);
        Browser1.Navigate(websites[1]);
     }

     if (Browser1.Url.ToString().Contains(websites[1])) 
     {
        while (Browser1.ReadyState != WebBrowserReadyState.Complete) 
        {
            Application.DoEvents();
        }
        Browser1.Document.GetElementById("mainsearchtext").SetAttribute("value", inputs[1]);
        Browser1.Navigate(websites[2]);
     }
     if (Browser1.Url.ToString().Contains(websites[2])) 
     {
         Browser1.Document.GetElementById("search-input").SetAttribute("value", inputs[2]);
     }

SCOND方式;

int n;
private void Browser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
           n++;
         if (n == 1) 
         {

            Browser1.Document.GetElementById("email").SetAttribute("value", inputs[0]);
            Browser1.Navigate(websites[1]);
         }

         if (n == 2) 
         {

            Browser1.Document.GetElementById("mainsearchtext").SetAttribute("value", inputs[1]);
            Browser1.Navigate(websites[2]);
         }
         if (n == 3) 
         {
             Browser1.Document.GetElementById("search-input").SetAttribute("value", inputs[2]);
         }

第二种方法的问题是n有时可能与预期不同。

我有加载第二和第三个网站的问题。我还尝试了一些其他的方法,但都不起作用。所以我的目标是一页接一页地打开每一页,在每一页上做一些任务。如果您有别的办法,我将不胜感激。

等一下,用c#在几个网站上做点什么

也许可以试试这样做:

int current = 0;
string[] websites = { "www.facebook.com", "www.bug.hr", "www.htmlgoodies.com" };
string[] elements = { "email", "mainsearchtext", "search-input" };
string[] inputs = { "first", "second", "third" };
private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.Navigate(websites[current]);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    timer1.Interval = 250;
    timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
    {
        timer1.Stop();
        webBrowser1.Document.GetElementById(elements[current]).SetAttribute("value", inputs[current]);
        if (++current < websites.Length)
        {
            webBrowser1.Navigate(websites[current]);
        }
    }
}