Reputation: 640
I am using the beta version of selenium webdriver(4.0.0) for microsoft edge. I can't figure out how to save the data as pdf that is in print window, which opens after I click on a button in main window
var found = await driver.WaitForElementToAppear(".//font[contains(text(),'Uploaded')]", 180, token);
if(found)
{
//All buttons , which when clicked opens Print window
var ebrcElements = driver.ElementsByXpath(".//input[@type='submit']");
foreach (var ebrcElement in ebrcElements)
{
ebrcElement.Click();//Opens the new window
Thread.Sleep(2000);
driver.SwitchToNewWindow();//Switches to the last window
//The window has "Print" and "Cancel" button
driver.CloseCurrentWindow();
driver.SwitchToFirstWindow();
}
}
Upvotes: 0
Views: 846
Reputation: 640
Since the window that opens when print button is clicked, is an os dialog box, it cannot be accessed directly from selenium. The way I solved the problem was using P/Invoke. Using visual studio's spy++ tool it was easy to find the window handle and its caption.
Next I defined method
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
and Called it like,
var saveHandle = FindWindowByCaption(IntPtr.Zero, "Save Print Output As");
Next step was to bring the window to foreground using the handle. For that I used the method
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
Last step was to simulate the keyboard and mouse operations. For that I used library InputSimulator. It can be downloaded using nuget package manager
var ins = new InputSimulator();
ins.Keyboard.TextEntry($"ebrc{irandom.NextDouble()}");
Thread.Sleep(2000);
ins.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);
ins.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);
ins.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);
Upvotes: 1