【问题标题】:WatiN error "hwnd cannot be IntPtr.Zero or null"WatiN 错误“hwnd 不能是 IntPtr.Zero 或 null”
【发布时间】:2014-10-16 16:42:56
【问题描述】:

我在使用 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();
    }
}

【问题讨论】:

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

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2010-11-30
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    相关资源
    最近更新 更多