[英]Automate file save in microsoft edge using selenium
我正在为 microsoft edge 使用 selenium webdriver(4.0.0) 的测试版。 我不知道如何将数据保存为打印窗口中的 pdf 格式,该窗口在我单击主窗口中的按钮后打开
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();
}
}
由于单击打印按钮时打开的窗口是一个 os 对话框,因此无法从 selenium 直接访问它。 我解决问题的方法是使用 P/Invoke。 使用 Visual Studio 的 spy++ 工具很容易找到窗口句柄及其标题。
接下来我定义了方法
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
并称它为,
var saveHandle = FindWindowByCaption(IntPtr.Zero, "Save Print Output As");
下一步是使用手柄将窗口置于前台。 为此,我使用了方法
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
最后一步是模拟键盘和鼠标操作。 为此,我使用了库 InputSimulator。 可以使用nuget包管理器下载
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.