2012-09-13 5 views
5

Web browser 컨트롤을 사용하여 VS 2010 C#에서 Windows Forms 응용 프로그램을 개발하고 있습니다. 내 목표는이 웹 사이트에서 탐색을 자동화하는 것입니다. 그러나 특정 시점의 메신저에서 OK 버튼을 누를 때까지 자동화를 중지하는 자바 스크립트 경고가 팝업됩니다. 팝업이 나타나면 입력 프레스를 시뮬레이션하여 문제를 해결했지만 응용 프로그램이 작동하도록하려면 계속 초점을 유지해야합니다. 내 질문은 웹 사이트에서이 사용자 지정 javascript 경고를 죽일 수있는 방법이 있습니까 (나는 측면에 대한 액세스 권한이 없으므로 클라이언트 쪽에서 죽이지 않음) 그래서이 문제를 해결할 수있는 다른 방법이 있습니까? 표시되는 자바 스크립트 경고 (messagebox)는 오류가 아니며, 자바 스크립트 경고는 해당 웹 사이트 프로그래머가 어떤 이유로 든 넣었습니다.webbrowser 컨트롤 웹 사이트에서 자바 스크립트 알림을 중지합니다.

+1

약간의 인터넷 검색 결과 : http://josheinstein.com/blog/index.php/2010/01/webbrowser-control-prevent-window-alert/ –

답변

0

Navigated 이벤트를 소비하고 DocumentText을 가로 채기 전에 페이지로드를 시도하여 alert(...); 참조를 제거 할 수 있습니다. 은 MSDN에 Navigated 페이지에서

:

WebBrowser 제어가 새 문서를 탐색했을 때 알림을받을 수있는 Navigated 이벤트를 처리합니다. Navigated 이벤트가 발생하면 새 문서가로드되기 시작합니다. 즉, Document, DocumentTextDocumentStream 속성을 통해로드 된 콘텐츠에 액세스 할 수 있습니다.

using System.Windows.Forms; 
using System.Text.RegularExpressions; 

namespace Your.App 
{ 
    public class PopupSuppress 
    { 
     WebBrowser _wb; 
     public PopupSupress() 
     { 
      _wb = new WebBrowser(); 
      _wb.Navigated += new WebBrowserNavigatedEventHandler(_wb_Navigated); 
     } 

     void _wb_Navigated(object sender, WebBrowserNavigatedEventArgs e) 
     { 
      string alertRegexPattern = "alert\\([\\s\\S]*\\);"; 
      //make sure to only write to _wb.DocumentText if there is a change. 
      //This will prompt a reloading of the page (and another 'Navigated' event) [see MSDN link] 
      if(Regex.IsMatch(_wb.DocumentText, alertRegexPattern)) 
       _wb.DocumentText = Regex.Replace(_wb.DocumentText, alertRegexPattern, string.Empty); 
     } 
    } 
} 

소스/자료 :

  • http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigated.aspx
  • http://regexpal.com/
    • 이 (죄송합니다, 더했지만, SO 나만이 링크를 게시 할 것입니다) 여기
  • 는 일부 코드입니다