2015-06-30 148 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