Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “WatiN错误”;hwnd不能为IntPtr.Zero或null“;_C#_Internet Explorer_Console_Console Application_Watin - Fatal编程技术网

C# “WatiN错误”;hwnd不能为IntPtr.Zero或null“;

C# “WatiN错误”;hwnd不能为IntPtr.Zero或null“;,c#,internet-explorer,console,console-application,watin,C#,Internet Explorer,Console,Console Application,Watin,我在使用WatiN执行浏览器自动化时遇到此错误 代码在DownloadIeFile方法的下一行失败 AutomationElementCollection dialogElements = AutomationElement.FromHandle(windowDialog.Hwnd).FindAll(TreeScope.Children, Condition.TrueCondition); 奇怪的是,如果机器上已经有IE open的实例,那么代码运行良好。我已尝试通过以下方式模仿这一点: 1

我在使用WatiN执行浏览器自动化时遇到此错误

代码在DownloadIeFile方法的下一行失败

AutomationElementCollection dialogElements = AutomationElement.FromHandle(windowDialog.Hwnd).FindAll(TreeScope.Children, Condition.TrueCondition);
奇怪的是,如果机器上已经有IE open的实例,那么代码运行良好。我已尝试通过以下方式模仿这一点:

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

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

看起来像windowDialog.Hwnd是零。当您调用DownloadFile时,新的IE调用没有启动Internet Explorer,因此没有窗口句柄,这可能是一种竞争条件吗?这就解释了为什么当你只是在现有IE流程中添加一个标签时,当IE实例运行时,它会起作用,因此启动时间更短。我以前从未使用过System.Windows.Automation-当浏览器对象总是传递给DownloadIeFile方法时,怎么会发生这种情况?感谢您的回复Allan-我不确定这是否是问题所在,因为在调用DownloadIeFile方法之前,我已经打开并操作了IE对象。。。