Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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();
                        }
                    }
                }
            }
        }
Posted
Comments
Dmitri Nеstеruk 25-Oct-15 6:48am    
Err, actually, HwndSource.FromHwnd simply returns `null` so your solution doesn't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900