WatiN按IE上的保存按钮 [英] WatiN Pressing Save Button On IE

查看:71
本文介绍了WatiN按IE上的保存按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我正在尝试更改此链接上的代码:



< a href =http://stackoverflow.com/questions/7500339/how-to-test-file-download-with-watin-ie9> http://stackoverflow.com/questions/7500339/how-to-test -file-download-with-watin-ie9 [ ^ ]



我希望最终结果会点击互联网上的保存按钮使用WaTiN成功启动下载后的资源管理器窗口。不幸的是我不得不改变一些东西来实际编译,因为代码是两年了,但是我没有从方法的第二行得到任何东西因为源仍然设置为null。



有什么建议吗?如何修复此方法(代码如下)或如何使用WatiN按IE上的保存按钮开始下载?



非常感谢



Hey,

I am currently trying to change the code on this link:

http://stackoverflow.com/questions/7500339/how-to-test-file-download-with-watin-ie9[^]

Which I hope as an end result will end up clicking the "Save" button on Internet Explorer window after I have successfully initiated a download using WaTiN. Unfortunately I had to change a few things around to actually get it to compile as the code is two years old, however I am not getting anything from the second line in the method as source is still set to null.

Any suggestions? Either how to fix this method(code bellow) or how to use WatiN to press the save button on IE to start a download?

Many Thanks

public void DownloadIEFile(Browser browser)
        {
            HwndSource source;

            source = HwndSource.FromHwnd(WatiN.Core.Native.Windows.NativeMethods.GetWindow(browser.hWnd, 5));
            Window windowMain = source.RootVisual as Window;
            System.Windows.Automation.TreeWalker trw = new System.Windows.Automation.TreeWalker(System.Windows.Automation.Condition.TrueCondition);
            System.Windows.Automation.AutomationElement mainWindow = trw.GetParent(System.Windows.Automation.AutomationElement.FromHandle(browser.hWnd));

            source = HwndSource.FromHwnd(WatiN.Core.Native.Windows.NativeMethods.GetWindow(new WindowInteropHelper(windowMain).Handle, 5));
            Window windowDialog = source.RootVisual as Window;
            // if doesn't work try to increase sleep interval or write your own waitUntill method
            Thread.Sleep(1000);
            windowDialog.Activate();
            System.Windows.Automation.AutomationElementCollection amc = System.Windows.Automation.AutomationElement.FromHandle(new WindowInteropHelper(windowDialog).Handle).FindAll(System.Windows.Automation.TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);

            foreach (System.Windows.Automation.AutomationElement element in amc)
            {
                // You can use "Save ", "Open", ''Cancel', or "Close" to find necessary button Or write your own enum
                if (element.Current.Name.Equals("Save"))
                {
                    // if doesn't work try to increase sleep interval or write your own waitUntil method
                    // WaitUntilButtonExsist(element,100);
                    Thread.Sleep(1000);
                    System.Windows.Automation.AutomationPattern[] pats = element.GetSupportedPatterns();
                    // replace this foreach if you need 'Save as' with code bellow
                    foreach (System.Windows.Automation.AutomationPattern pat in pats)
                    {
                        // '10000' button click event id 
                        if (pat.Id == 10000)
                        {
                            System.Windows.Automation.InvokePattern click = (System.Windows.Automation.InvokePattern)element.GetCurrentPattern(pat);
                            click.Invoke();
                        }
                    }
                }
            }
        }

推荐答案

这篇关于WatiN按IE上的保存按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆