watin - Automate of file download in IE 11 using c# -
i trying window handler , press save button. found couple of examples on ie8 & 9. code doesn't works on ie 11.
const int bm_click = 0x00f5; [dllimport("user32.dll", setlasterror = true)] static extern intptr findwindow(string lpclassname, string lpwindowname); [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool setforegroundwindow(intptr hwnd); [dllimport("user32.dll", setlasterror = true)] static extern intptr setactivewindow(intptr hwnd); [dllimport("user32.dll", charset = charset.auto)] public static extern intptr findwindowex(intptr parent, intptr next, string sclassname, intptr swindowtitle); [dllimport("user32.dll", setlasterror = true)] static extern intptr findwindowex(intptr hwndparent, intptr hwndchildafter, string lpszclass, string lpszwindow); [dllimport("user32.dll", charset = charset.auto)] static extern intptr sendmessage(intptr hwnd, uint32 msg, intptr wparam, intptr lparam); [dllimport("user32.dll", exactspelling = true, charset = charset.auto)] public static extern uint getdlgctrlid(intptr hwnd); [dllimport("user32.dll", charset = charset.auto)] public static extern intptr sendmessage(intptr hwnd, int msg, int wparam, intptr lparam); //hdialog - handle of dialog window. idbtn - id of button public static bool clickbuttonondialog(intptr hdialog, uint32 idbtn) { intptr res = intptr.zero; uint id; intptr hokbtn = intptr.zero; int attempt = 0; { thread.sleep(300); //searching button hokbtn = findwindowex(hdialog, hokbtn, "button", intptr.zero); id = getdlgctrlid(hokbtn); attempt++; } while (id != idbtn && attempt < 20); if (!hokbtn.equals(intptr.zero)) { //click button res = sendmessage(hokbtn, (int)bm_click, 1, intptr.zero); } if (res.toint32() == 1) return true; return false; } public static void findandsave() { intptr hokbtn = intptr.zero; uint message = 0xf5; intptr hwnd = findwindow(null, "internet explorer"); hokbtn = findwindowex(hwnd, hokbtn, "button", "cancel"); sendmessage(hokbtn, (int)message, 1, intptr.zero);
i able download , close file download dialog box using below code
[dllimport("user32.dll", setlasterror = true)] static extern intptr findwindow(string lpclassname, string lpwindowname); static void downloadfile(ie browser) { browser.link(find.bytext("download")).clicknowait(); thread.sleep(1000); automationelementcollection dialogelements = automationelement.fromhandle(findwindow(null, "internet explorer")).findall(treescope.children, condition.truecondition); foreach (automationelement element in dialogelements) { if (element.current.name.equals("save")) { var invokepattern = element.getcurrentpattern(invokepattern.pattern) invokepattern; invokepattern.invoke(); } } }
Comments
Post a Comment