我正在嘗試使用 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}");
uj5u.com熱心網友回復:
正常的 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);
- 長手
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();
所以試試這些,但不知道為什么
- 如果它在頁面上有一個 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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/352821.html
