2011-03-11 4 views
0

웹 브라우저를 사용하고 싶습니다. 링크를 클릭하고 25 초를 기다려야합니다.C# Web Browser Bot 질문

private void webBrowserMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) // This is only way It worked for me. 
    { 
     if (webBrowserMain.Url.AbsoluteUri == @"http://www.clix-cents.com/pages/clickads") 
     { 
      Regex regAddId = new Regex("onclick=\\'openad\\(\"([\\d\\w]+)\"\\);", RegexOptions.IgnoreCase); // Find link and click it. 
      if (regAddId.IsMatch(webBrowserMain.DocumentText)) 
      { 
       string AddId = regAddId.Match(webBrowserMain.DocumentText).Groups[1].ToString(); 
       webBrowserMain.Navigate(@"http://www.clix-cents.com/pages/clickads?h=" + AddId); 
      } 
     } 
     else if (webBrowserMain.Url.AbsoluteUri.Contains("http://www.clix-cents.com/pages/clickads?h=")) // up to there everything is ok. But problem starts here. 
     { 
      Thread.Sleep(25000); // It pouses whole thread and browser, so timer in browser is not counting down. 
      Regex regCaptchaCode = new Regex("src=\\'/pages/captcha\\?t=c&s=([\\d\\w\\W]+)\\'", RegexOptions.IgnoreCase); 
      if (regCaptchaCode.IsMatch(webBrowserMain.DocumentText)) 
      { 
       pictureBox1.ImageLocation = @"http://www.clix-cents.com/pages/captcha?t=c&s=" + regCaptchaCode.Match(webBrowserMain.DocumentText).ToString(); 
      } 
     } 
    } 

그런 식으로 봇을 작성하는 방법은 무엇입니까? 나는 모른다.

답변

3

바퀴를 재발 명하지 마십시오. 이미 테스트를 위해 사용되는 자동화 솔루션에 적합한 솔루션이 이미 있습니다. WatiN. WatiN 페이지에서

코드 예제 :

[Test] 
public void SearchForWatiNOnGoogle() 
{ 
    using (var browser = new IE("http://www.google.com")) 
    { 
    browser.TextField(Find.ByName("q")).TypeText("WatiN"); 
    browser.Button(Find.ByName("btnG")).Click(); 

    Assert.IsTrue(browser.ContainsText("WatiN")); 
    } 
} 
1

아마 타이머를 사용할 수 있습니다. 예 :

private Timer t = new Timer(); 
private string nextUrl = ""; 
private void buttonStart_Click(object sender, EventArgs e) 
{ 
    t.Interval = 2500; 
    t.Tick += new EventHandler(t_Tick); 
} 

void t_Tick(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(nextUrl)) 
     webBrowser1.Navigate(nextUrl); 
    else 
    { 
     Regex regCaptchaCode = new Regex("src=\\'/pages/captcha\\?t=c&s=([\\d\\w\\W]+)\\'", RegexOptions.IgnoreCase); 
     if (regCaptchaCode.IsMatch(webBrowserMain.DocumentText)) 
     { 
      pictureBox1.ImageLocation = @"http://www.clix-cents.com/pages/captcha?t=c&s=" + regCaptchaCode.Match(webBrowserMain.DocumentText).ToString(); 
     } 
    } 
} 
private void webBrowserMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) // This is only way It worked for me. 
{ 
    if (webBrowserMain.Url.AbsoluteUri == @"http://www.clix-cents.com/pages/clickads") 
    { 
     Regex regAddId = new Regex("onclick=\\'openad\\(\"([\\d\\w]+)\"\\);", RegexOptions.IgnoreCase); // Find link and click it. 
     if (regAddId.IsMatch(webBrowserMain.DocumentText)) 
     { 
      string AddId = regAddId.Match(webBrowserMain.DocumentText).Groups[1].ToString(); 
      nextUrl = @"http://www.clix-cents.com/pages/clickads?h=" + AddId; 
      t.Start(); 
     } 
    } 
    else if (webBrowserMain.Url.AbsoluteUri.Contains("http://www.clix-cents.com/pages/clickads?h=")) // up to there everything is ok. But problem starts here. 
    { 
     nextUrl = ""; 
     t.Start(); 
    } 
} 

실제 구현은 사이트의 실제 데이터와 사용 방법에 따라 달라집니다. 모든 링크가 한 페이지에 있고 각 링크를 열고 싶다면 모든 링크를 구문 분석하고 목록에 저장할 수 있습니다. 그런 다음 타이머를 시작하십시오. 각 틱에서 1 개의 아이템을 열 수 있습니다.