8

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.

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.

TLDR: I have found http://code.tutsplus.com/tutorials/testing-javascript-with-phantomjs--net-28243 and this is what i want to do but i don't know how to use the javascript in my c# app.

3
  • This question already has an answer. stackoverflow.com/questions/3422262/… Commented Jul 7, 2014 at 16:16
  • 1
    damn man i thought i did enough searching thanks and sorry for wasting your time Commented Jul 7, 2014 at 16:17
  • Also, you may need to maximize your browser window. I noticed with the phantomJS driver that the screen was not maximized by default. Read the second answer from this question: stackoverflow.com/questions/3189430/… Commented Jul 7, 2014 at 16:17

1 Answer 1

11

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.

However, here is how to take screenshot using PhantomJSDriver in C# in general:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

For those looking at this, the TakeScreenshot() is an extension method found in OpenQA.Selenium.Support.Extensions
To get this to work, I had to install the PhantomJS, Selenium WebDriver, and Selenium WebDriver Support Classes NuGet packages, and add the OpenQA.Selenium.PhantomJS, OpenQA.Selenium.Support.Extensions, and System.Drawing.Imaging namespaces to my class. After that it worked perfectly. :-)
You also need to include the namespace, so using OpenQA.Selenium.Support.Extensions;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.