2015-06-30 147 views
1

我正在製作程序以自動進入網站並打印頁面,似乎無法使其工作,我試過硒鉻驅動程序,問題是不起作用。我試圖action.sendkeys鍵ctrl + shift + p無濟於事,最大的問題是打印預覽彈出。 我試着發送JavaScript命令:window.print(),但在我的方式中,chrome的打印預覽方式正常,因爲您需要按Enter鍵。有沒有在JavaScript的方式來模擬按下ENTER鍵?幫助將不勝感激。以編程方式打印網頁

回答

2

那麼經過一番研究,我發現這個video,如果你可以添加這些開關:「--kiosk --kiosk-printing」,給chrome驅動啓動,它會自動跳過打印預覽提示,就像視頻中所示。 另外,我在最新版本的SRWare鐵(鉻叉)上測試了它,並且它工作正常。

如果您正在使用C#來使你的程序,然後有一個簡單的解決方案:

private void Button1_Click(object sender, EventArgs e) 
    { 
     PrintHelpPage(); 
    } 

    private void PrintHelpPage() 
    { 
     // Create a WebBrowser instance. 
     WebBrowser webBrowserForPrinting = new WebBrowser(); 

     // Add an event handler that prints the document after it loads. 
     webBrowserForPrinting.DocumentCompleted += 
      new WebBrowserDocumentCompletedEventHandler(PrintDocument); 

     // Set the Url property to load the document. 
     webBrowserForPrinting.Url = new Uri(@"http://www.google.com"); //This is what you want to change 
    } 

    private void PrintDocument(object sender, 
     WebBrowserDocumentCompletedEventArgs e) 
    { 
     // Print the document now that it is fully loaded. 
     ((WebBrowser)sender).Print(); 

     // Dispose the WebBrowser now that the task is complete. 
     ((WebBrowser)sender).Dispose(); 
    } 

發現在Here答案,這使用WebBrowser控件導航到一個特定的URL,可以是本地網址或來自互聯網,並使用您的默認打印機進行打印。

+0

非常感謝,我今天在這個問題上苦苦掙扎了3個小時,並沒有意識到要深入c#的webbrowswer控制,就像在你手中拿着眼鏡一邊搜索眼鏡一樣。我按「接受」,這是我需要做的嗎?我在回答,編輯評論和接受答案方面是新的。 – user3150255

+0

是的,你做得很好:) – Paripsky