繁体   English   中英

Control + P 不能使用 selenium C#

[英]Control + P not working using selenium C#

我正在尝试使用 selenium C# 打印网页并另存为 pdf。 但是Ctrl + P键不起作用。 我尝试了以下所有可能的方法

1 - SendKeys.SendWait(Keys.Control + "P");

2 -

Actions vaction = new Actions(driver);
            vaction.KeyDown(Keys.Control);
            vaction.SendKeys("p");
            vaction.KeyUp(Keys.Control);
            vaction.Build().Perform();

3 - 出现使用 javascript 打印对话框,但之后输入键无法将页面保存为 pdf

((IJavaScriptExecutor)driver).ExecuteScript("window.print()");
        driver.SwitchTo().Window(driver.WindowHandles.Last());
        SendKeys.SendWait("{ENTER}");

正常的 sendkeys 语法是

1)

SendKeys.SendWait("^p"); // or ("^{p}")

其中 ^ 是 Control 的符号,因此 {^} 是文字 ^

通常应用等待,因为进入会为时过早

    SendKeys.SendWait("^p");
    // give it time to render the print
    System.Threading.Thread.Sleep(5000);
    SendKeys.SendWait("~");

    // give it time to spool onto the printer
    System.Threading.Thread.Sleep(5000);
  1. 长手
keysEventInput.Click();

            Actions vAction = new Actions(driver);
            IAction pressControl = vAction.KeyDown(keysEventInput, Keys.Control).Build();
            pressControl.Perform();

            IAction sendLowercase = vAction.SendKeys(keysEventInput, "p").Build();
            sendLowercase.Perform();

            IAction releaseControl = vAction.KeyUp(keysEventInput, Keys.Control).Build();
            releaseControl.Perform();

所以试试这些,但不知道为什么

  1. 如果它在页面上有一个 findelement 按钮到 button.click() 或 element.sendKeys(Keys.ENTER);

也许https://github.com/MicrosoftEdge/WebView2Feedback/issues/1471#issuecomment-871315418 有帮助

还考虑

// press Enter programmatically - the print dialog must have focus, obviously
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM