WatiN错误“hwnd不能是IntPtr.Zero或null”

时间:2014-10-16 16:42:57

标签: c# internet-explorer console console-application watin

使用WatiN执行浏览器自动化时出现此错误。

I am using this solution from a previous thread in order to acess the Save As dialog

代码在DownloadIeFile方法的以下行失败。

AutomationElementCollection dialogElements = AutomationElement.FromHandle(windowDialog.Hwnd).FindAll(TreeScope.Children, Condition.TrueCondition);

奇怪的是,如果机器上已经打开IE实例,代码运行正常。我试过仿效:

1]创建临时浏览器的实例
2]创建浏览器对象     用于操纵
3]关闭两个浏览器实例。

这也失败了。

仅供参考我这是我正在使用的代码:

class UpdateDiaryFigures
{ 


    public static void Start()
    {


        System.Threading.Thread.Sleep(1000);

        IE browser = new IE("http://www.example.com");
        //browser.Visible = false;
        Login(browser);

        string test = GetExport(browser, "AgentOpen", "01/10/2014", DateTime.Today.ToString("dd/MM/yyyy"), "Agent Name Ltd");

        //browser.Dispose();

    }


    static string GetExport(IE browser, string exportName, string fromDate, string toDate, string ddlValue)
    {

        browser.GoTo("http://www.example.com/ExcelExport.aspx");

        string open = "";

        switch (exportName)
        {
            case "AgentOpen":
            case "AgentClosed":
                if (exportName == "AgentClosed") { open = "Yes"; } else { open = "No"; }
                browser.TextField(Find.ById("txtAgentFromDate")).Value = fromDate;
                browser.TextField(Find.ById("txtAgentToDate")).Value = toDate;
                browser.SelectList(Find.ById("cboAgentCleared")).Select(open);
                browser.SelectList(Find.ById("cboAgent")).Select(ddlValue);
                browser.Image(Find.ById("btnPrintAgentRpt")).Click();
                break;
            case "AllAgentOpen":
                break;
            case "AllAgentClosed":
                break;
            case "CourtOpen":
            case "CourtClosed":
                break;
        }

        string filename = @"c:\Downloads\" + exportName
        browser.DownloadIeFile(filename);
        return filename;

    }

    static void Login(IE browser)
    {
        browser.TextField(Find.ByName("ctl00$cphMainContent$txtCompanyID")).Value = "ID";
        browser.TextField(Find.ByName("ctl00$cphMainContent$txtUserID")).Value = "user";
        browser.TextField(Find.ByName("ctl00$cphMainContent$txtPassword")).Value = "pass";
        browser.Button(Find.ByName("ctl00$cphMainContent$btnLogin")).Click();
    }
}

1 个答案:

答案 0 :(得分:1)

即使您只能看到一个IE窗口,也可能会运行多个iexplore.exe进程。例如iexplore.exe和iexplore.exe * 32