简体   繁体   中英

WatiN error “hwnd cannot be IntPtr.Zero or null”

I am getting this error when performing browser automation using WatiN.

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

The code fails at the following line of the DownloadIeFile method.

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

What's bizarre is that the code runs fine if there was already an instance of IE open on the machine. I have tried emulating this by:

1] create instance of a temporary browser
2] create browser object for manipulation
3] close both browser instances.s.

and this also fails.

FYI this is the code I am using:

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();
    }
}

You may have multiple iexplore.exe process running even if you can only see one IE window. For example iexplore.exe and iexplore.exe*32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM