javascript 在 C# 中使用 PhantomJS 获取屏幕截图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24615079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting screenshot using PhantomJS in C#
提问by Darthlemi
I have added PhantomJS and Selenium to my C# console app and i want to take a screen shot of the browser when it gets to a specific element. The reason is because for some reason when i use the ChromeDriver it works fine, but when i use PhantomJS it fails on a few elements.
我已经将 PhantomJS 和 Selenium 添加到我的 C# 控制台应用程序中,我想在浏览器到达特定元素时截取它的屏幕截图。原因是由于某种原因,当我使用 ChromeDriver 时它工作正常,但是当我使用 PhantomJS 时它在一些元素上失败。
I guess i need an introduction on how to take a screenshot in C# using phantomjs. I have looked around on the internet and it looks like everyone is using java scripts to do this. The problem i am having is i don't know how to integrate the java scripts into my C# app and then use that with phantomJS to get the screen shot. If i can get some help on how to do this it would be very nice.
我想我需要介绍一下如何使用 phantomjs 在 C# 中截取屏幕截图。我在互联网上环顾四周,看起来每个人都在使用 java 脚本来做到这一点。我遇到的问题是我不知道如何将 java 脚本集成到我的 C# 应用程序中,然后将它与 phantomJS 一起使用来获取屏幕截图。如果我能得到一些关于如何做到这一点的帮助,那就太好了。
TLDR: I have found http://code.tutsplus.com/tutorials/testing-javascript-with-phantomjs--net-28243and this is what i want to do but i don't know how to use the javascript in my c# app.
TLDR:我找到了http://code.tutsplus.com/tutorials/testing-javascript-with-phantomjs--net-28243,这就是我想要做的,但我不知道如何在我的C# 应用程序。
回答by Yi Zeng
As you mentioned that you have code working for Chrome already, it's better to post it, in order to show what exactly you are after.
正如您提到的,您已经有适用于 Chrome 的代码,最好将其发布,以显示您所追求的内容。
However, here is how to take screenshot using PhantomJSDriver
in C# in general:
但是,以下是PhantomJSDriver
一般在 C# 中使用截屏的方法:
var driver = new PhantomJSDriver();
driver.Manage().Window.Maximize(); // optional
driver.Navigate().GoToUrl("http://stackoverflow.com");
driver.TakeScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);
driver.Quit();
Note that you need to reference WebDriver.Support.dll
and System.Drawing
in your project.
请注意,您需要在您的项目中引用WebDriver.Support.dll
和System.Drawing
。