C#探测并捕获Javascript警报()和确认()

C#探测并捕获Javascript警报()和确认(),c#,.net,internet-explorer,automation,alert,C#,.net,Internet Explorer,Automation,Alert,所以我已经做了一段时间的研究,我已经走到了死胡同。我正在做一些IE自动化。在C#/.NET中,如何探测和使用javascript alert()或confirm()以便抓取其文本并单击OK\Cancel按钮 更新: 我需要重申:我需要能够从alert()或confirm()中提取和验证文本,并发送确定或取消单击。这种测试的一个例子是确保当我单击delete时,confirm()没有说“你确定你想去墨西哥吗?”或者除了正确的消息之外的任何其他内容 为了以防万一,让我重申一下:就本测试而言,我对相关

所以我已经做了一段时间的研究,我已经走到了死胡同。我正在做一些IE自动化。在C#/.NET中,如何探测和使用javascript alert()或confirm()以便抓取其文本并单击OK\Cancel按钮

更新

我需要重申:我需要能够从alert()或confirm()中提取和验证文本,并发送确定或取消单击。这种测试的一个例子是确保当我单击delete时,confirm()没有说“你确定你想去墨西哥吗?”或者除了正确的消息之外的任何其他内容

为了以防万一,让我重申一下:就本测试而言,我对相关网站的来源拥有零控制权


最后,我使用的是
SHDocVw.InternetExplorer

我认为您必须使用FindWindow、sendmages等。这就是它的对话框处理方式。

FindWindow-将让您接近;您确实想要一个窗口句柄,但您不确定如何找到它

最好使用EnumWindows。下面是一个例程示例,您可以每隔一两秒钟运行一次,它将为您提供有关系统上每个窗口的大量信息,然后您可以对其进行分析。您可以检查窗口的类型、其中的文本、获取按钮,并向右侧发送单击消息。它会让你做你希望能做的一切

要获取文本,请发送wm_gettext,对于按钮,您可以单击wm_而不受影响

对不起,C。我能找到的下一个最好的例子是LISP。花点时间阅读代码,你就会明白这个想法是什么。匹配函数有助于确定文本是否正确,而不必确切地知道它

如果你能在c#中做到这一点,我打赌你会得到一个更短的解决方案。(能否在EnumWindows的输出上使用LINQ?;-)

#包括
#包括
#包括
#包括
#包括“resource.h”
#定义ET_启动1
#定义ET_窗口打开2
#定义ET_运行端3
#定义ET_USEREVENT 4
#定义M_星1
#定义M_问题2
类型定义结构窗口列表
{
内描述线;
HWND-HWND;
字符*stName;
字符*stTitle;
char*stClassName;
char*stParentName;
结构WindowList_s*wlParentPtr;
char*stChildName;
结构WindowList_s*wlChildPtr;
int bWarned;
结构WindowList_s*下一步;
}窗口列表\u t;
////////////////
外部长g_runids[];
外部窗口列表\u t*g\u窗口列表;
外部事件列表\u t*g\u事件列表;
char-stWTx[2000];
char-stWCl[200];
char-stPTx[2500];
char-stPCl[200];
int BEVENTERY=0;//意味着我们正在处理一个事件。
HWND-LogWinParent;
void FixIt(字符*buf,无符号整数大小)
{
inti,j,bump;
int-ir;
炭铬;
字符十六进制[10];
//防止特殊角色通过
//替换w/单字符通配符
对于(i=0;buf[i];i++)
{
cr='\0';
ir=0;
通气量=1;
开关(buf[i])
{
大小写“\0”:cr='0';中断;
案例“\\”:cr=“\\”;中断;
大小写“\n”:cr='n';中断;
大小写“\t”:cr='t';break;
大小写“\a”:cr='a';break;
大小写“\b”:cr='b';break;
大小写“\f”:cr='f';break;
大小写“\r”:cr='r';break;
案例M_星:cr='*';中断;
案例M_问题:cr='*';中断;
案例“'”:cr=”;中断;
违约:
if(buf[i]=127)
{
通气量=2;
ir=buf[i];
}
其他的
凹凸=0;
}
如果(碰撞)
{
对于(j=strlen(buf)+bump+1>=siz-1?siz-1:strlen(buf)+1;
j> i;j——)
buf[j+凹凸]=buf[j];
buf[siz-1]='\0';
if(cr)
{
buf[i++]='\\';
buf[i]=cr;
}
if(ir)
{
sprintf(十六进制,\\x%00H),红外;
memcpy(buf+i,十六进制,4);
i+=4;
}
}
}
}
无效LogWin(HWND hw)
{
静态长ParentNum=0,ChildNum=0;
GetWindowText(hw、stWTx、sizeof(stWTx)-1);
GetClassName(hw、stWCl、sizeof(stWCl)-1);
FixIt(stWTx,sizeof(stWTx));
FixIt(stWCl,sizeof(stWCl));
if(GetParent(hw)=LogWinParent)
{
ChildNum++;
sprintf(stPTx,“窗口对象%ld.%ld\%s\%s”
“父%ld”、ParentNum、ChildNum、stWTx、stWCl、ParentNum);
}
else if(hw==LogWinParent)
{
ChildNum=0;
ParentNum++;
sprintf(stPTx,“窗口对象%ld\%s\\%s\”,
ParentNum、stWTx、stWCl);
}
其他的
{
ChildNum++;
sprintf(stPTx,“窗口对象%ld.%ld\%s\”\%s\“孙子”,
ParentNum、ChildNum、stWTx、stWCl);
}
记录(stPTx);
}
BOOL回调EnumChildProc4Logging(HWND-HWND,LPARAM-LPARAM)
{
罗格温(hwnd);
返回1;
}
void LogCurrentWindows(void)
{
静态int-bEntry=0;
静态HWND hwCurrentFocus=0;
如果(本特利)
返回;
bEntry=1;
LogWinParent=GetForegroundWindow();
if(LogWinParent!=hwCurrentFocus)
{
hwCurrentFocus=LogWinParent;
LogWin(LogWinParent);
EnumChildWindows(LogWinParent,EnumChildProc4Logging,0);
}
bEntry=0;
}
/******************************************************************/
/*match()-解析通配符字符串*/
/******************************************************************/
int匹配(char*模式,char*str)
{
圣帕特国际酒店;
patt=0;
st=0;
while(pattern[patt]| | str[st])/*一直走到“\0”处*/
{
如果((!pattern[patt]| | |!str[st])&pattern[patt]!=M|u星)
返回0;/*如果在'\0'处有一个失败*/
if(模式[patt]==M_星)
{
patt++;
while(str[st]&&!match(pattern+patt,str+st))
st++;
}
else if(pattern[patt]!=M_问题和pattern[patt]!=str[st])
返回0;/*哦,不!!不匹配*/
其他的
{
patt++;
st++;
}
}
返回1;/*成功匹配*/
}
无效检查窗口列表(HWND HWND)
{
窗口列表*wl;
HWND hwndParent,hwndTarget;
静态字符buf[600];
//获取窗口的类和文本
GetWindowText(hwnd、stWTx、sizeof(stWTx)-1);
GetClassName(hwnd、stWCl、sizeof(stWCl)-1);
//获取父窗口的类和文本
hwndParent=GetParent(hwnd);
GetWindowText(hwndParent、stPTx、sizeof(stPTx)-1);
GetClassName(hwndParent、stPCl、sizeof(stPCl)-1);
//搜索通过
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "resource.h"

#define ET_STARTUP 1
#define ET_WINDOWOPEN 2
#define ET_RUNEND 3
#define ET_USEREVENT 4
#define M_STAR 1
#define M_QUESTION 2

typedef struct WindowList_s
{
 int iScriptLine;
 HWND hwnd;
    char *stName;
 char *stTitle;
 char *stClassName;
 char *stParentName;
 struct WindowList_s *wlParentPtr;
 char *stChildName;
 struct WindowList_s *wlChildPtr;
 int bWarned;
 struct WindowList_s *next;
} WindowList_t;


////////////////



extern long g_runids[];
extern WindowList_t *g_windowlist;
extern EventList_t *g_eventlist;
char stWTx[2000];
char stWCl[200];
char stPTx[2500];
char stPCl[200];
int bEventEntry=0; // means we're inside of processing an event.
HWND LogWinParent;

void FixIt(char *buf,unsigned int siz)
{
 int i,j,bump;
 int ir;
 char cr;
 char hex[10];
 // keep special characters from screwing up script by
 // replacing w/ single-character wildcard
    for (i=0;buf[i];i++)
 {
  cr='\0';
  ir=0;
  bump=1;
  switch(buf[i])
  {
  case '\0':cr='0';break;
  case '\\':cr='\\';break;
  case '\n':cr='n';break;
  case '\t':cr='t';break;
  case '\a':cr='a';break;
  case '\b':cr='b';break;
  case '\f':cr='f';break;
  case '\r':cr='r';break;
  case M_STAR:cr='*';break;
  case M_QUESTION:cr='*';break;
  case '"':cr='"';break;
  default:
   if (buf[i]<32 || buf[i]>=127)
   {
    bump=2;
    ir=buf[i];
   }
   else
    bump=0;
  }
  if (bump)
  {
   for (j=strlen(buf)+bump+1>=siz-1?siz-1:strlen(buf)+1;
    j>i;j--)
     buf[j+bump]=buf[j];
   buf[siz-1]='\0';
   if (cr)
   {
    buf[i++]='\\';
    buf[i]=cr;
   }
   if (ir)
   {
    sprintf(hex,"\\x%00H",ir);
    memcpy(buf+i,hex,4);
    i+=4;
   }
  }
 }
}
void LogWin(HWND hw)
{
 static long ParentNum=0,ChildNum=0;

 GetWindowText(hw,stWTx,sizeof(stWTx)-1);
 GetClassName(hw,stWCl,sizeof(stWCl)-1);
 FixIt(stWTx,sizeof(stWTx));
 FixIt(stWCl,sizeof(stWCl));
 if (GetParent(hw)==LogWinParent)
 {
  ChildNum++;
  sprintf(stPTx,"        WindowObject %ld.%ld \"%s\" \"%s\" "
   "Parent %ld",ParentNum,ChildNum,stWTx,stWCl,ParentNum);
 }
 else if (hw==LogWinParent)
 {
  ChildNum=0;
  ParentNum++;
  sprintf(stPTx,"    WindowObject %ld \"%s\" \"%s\"",
   ParentNum,stWTx,stWCl);
 }
 else 
 {
  ChildNum++;
  sprintf(stPTx,"            WindowObject %ld.%ld \"%s\" \"%s\" GRANDCHILD",
   ParentNum,ChildNum,stWTx,stWCl);
 }
 log_it(stPTx);
}

BOOL CALLBACK EnumChildProc4Logging(  HWND hwnd,LPARAM lParam )
{
    LogWin(hwnd);
 return 1;
}

void LogCurrentWindows(void)
{
 static int bEntry=0;
 static HWND hwCurrentFocus = 0;

 if (bEntry)
  return;
 bEntry=1;
    LogWinParent=GetForegroundWindow();
 if (LogWinParent!=hwCurrentFocus)
 {
  hwCurrentFocus=LogWinParent;
  LogWin(LogWinParent);
  EnumChildWindows(LogWinParent,EnumChildProc4Logging,0); 
 }
 bEntry=0;
}


/******************************************************************/
/* match() - parse wildcard strings .                             */
/******************************************************************/
int match(char *pattern,char *str)
{
    int patt,st;

    patt=0;
    st=0;
    while(pattern[patt] || str[st])  /* go until both at '\0' */
    {
        if ((!pattern[patt] || !str[st]) && pattern[patt]!=M_STAR)
            return 0;  /* fail if one at '\0' */
        if (pattern[patt]==M_STAR)
        {
            patt++;
            while (str[st] && !match(pattern+patt,str+st))
            st++;
        }
        else if (pattern[patt]!=M_QUESTION && pattern[patt]!=str[st])
            return 0;  /* Oh, No!! no match. */
        else
        {
            patt++;
            st++;
        }
    }
    return 1;  /* successful match */
}
void CheckWindowList(HWND hwnd)
{
 WindowList_t *wl;
 HWND hwndParent,hwndTarget;
 static char buf[600];
 // Get the window's class and text
 GetWindowText(hwnd,stWTx,sizeof(stWTx)-1);
 GetClassName(hwnd,stWCl,sizeof(stWCl)-1);
 // Get the Parent Window's class and text
 hwndParent=GetParent(hwnd);
 GetWindowText(hwndParent,stPTx,sizeof(stPTx)-1);
 GetClassName(hwndParent,stPCl,sizeof(stPCl)-1);

 // search thru window objects, fill in 
 // matching hwnds as appropriate
 for (wl=g_windowlist;wl;wl=wl->next)
 {
  hwndTarget=NULL; // separate variable enables 
                // warning on duplicate matches
  if (wl->wlChildPtr==NULL && wl->wlParentPtr==NULL)
  { // no parent/child requirements
   if (match(wl->stClassName,stWCl) && 
     match(wl->stTitle,stWTx) )
       hwndTarget=hwnd;
  }
  else if (wl->wlParentPtr)
  { // parent requirement - if I (hwnd) match the 
    // wl's class/title and the parent (hwndParent)
          // matches the wl's parentptr's class/title then
    // set 'me' as the window that matches and has
    // the proper parent requirement.
   if (match(wl->stClassName,stWCl) && 
     match(wl->stTitle,stWTx) &&
     match(wl->wlParentPtr->stClassName,stPCl) && 
     match(wl->wlParentPtr->stTitle,stPTx) )
       hwndTarget=hwnd;
  }
  else
  { // child requirement - if I (hwnd) match the child
    // requirement's stuff and the parent(hwndParent)
    // matches the wl's stuff then set the parent
    // as that wl's hwnd.
   if (match(wl->stClassName,stPCl) && 
     match(wl->stTitle,stPTx) &&
     match(wl->wlChildPtr->stClassName,stWCl) && 
     match(wl->wlChildPtr->stTitle,stWTx) )
       hwndTarget=hwndParent;
  }
  if (hwndTarget)
  {
   if (wl->hwnd && !wl->bWarned ) // log a warning on dup windows
   {
    sprintf(buf,"267.EnumChildProc4Search.10 "
     "Warning: more than one match for "
     "windowobject %s exists",wl->stName);
    log_it(buf);
    wl->bWarned =1;
   }
   else
    wl->hwnd=hwndTarget;
  }
 }
}
BOOL CALLBACK EnumChildProc4Search(  HWND hwnd,LPARAM lParam )
{
    CheckWindowList(hwnd);
    // return value of 1 means continue with rest of enumeration.
 return 1;
}

// to enumerate all... enumchild w/ NULL hwnd don't work???!
BOOL CALLBACK EnumWindowProc4Search(  HWND hwnd,LPARAM lParam )
{
    CheckWindowList(hwnd);
 EnumChildWindows(hwnd,EnumChildProc4Search,0);
 return 1;
}

// Check thru all windows and runIDs to see if a run is done
// or a window has opened. First emumerate the windows and running 
// programs and then see which events should be run.
// this function must not run if blocked, 'once' countdown
// has run out, or stopscript=1, or entry = 1
void RunEvents(void)
{
// Please keep in mind this program's error philosphoy when
// extending it, namely, catch all possible script errors
// when loading script, but do not display messageboxes for
// even the most serious errors when doing events, because 
// the idea is to catch errors during development and avoid
// errors at run time or in production!!
 static bEventEntry=0;
 WindowList_t *wl;
 EventList_t *el;
 ParmList_t *pl;
 char *st;
 int i;
 long lExitCode;

 // exit this routine if we shouldn't be here
    if (g_iStopScript || bEventEntry)
  return;
 bEventEntry=1;
 // clear all of the window handles in windowobject list
 for (wl=g_windowlist;wl;wl=wl->next)
 {
  wl->hwnd=NULL;
  wl->bWarned = 0;
 }

 // enumerate all windows in the system; repopulate all of
 // the window handles in the windowobject list
 EnumWindows(EnumWindowProc4Search,0);

 // go thru event list, calling any events that must 
 // be called; windows handles for any subsequent actions
 // will have been set by above code. Check for runs ending
 // to process runend events and search the windowobject list
 // to process windowopen events. 
 for (el=g_eventlist;g_eventlist && el;el=el->next)
 {
  pl=MakeParms(el->stEventTypeSourceLine);
  st=ParmValue(pl,2); // event name
  // check for windowopen events
  if (el->iEventType==ET_WINDOWOPEN)
  { // search for their open windows!!
   for (wl=g_windowlist;wl;wl=wl->next)
   {
    if (0==_stricmp(wl->stName,st) && wl->hwnd)
     RunActions(el);
    if (g_iStopScript)
     return; // quit doing actions/events ASAP.  }
   }
  }
  if (el->iEventType==ET_RUNEND)
  {
   sscanf(st,"%ld",&i);
   if (g_runids[i]!=-1 && g_runids[i]!=0)
   {
    GetExitCodeProcess((HANDLE)g_runids[i],&lExitCode);
    if (lExitCode != STILL_ACTIVE) 
    {
     RunActions(el);
     g_runids[i]=0;
    }
   }
  }
  KillParms(pl);
 }
 bEventEntry=0;
}

void RunStartup(void)
{
 EventList_t *el;

    if (bEventEntry)
  return;
 bEventEntry=1;
 // check stopscript in case of endscript in startup event!
 for (el=g_eventlist;g_eventlist && el && !g_iStopScript;el=el->next)
 {
  if (el->iEventType==ET_STARTUP)
   RunActions(el);
 }
 bEventEntry=0;
}