Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

PhantomJS can produce a transparent screenshot when the page leaves its background unset. Selenium can save that capture as a PNG, but its screenshot API does not guarantee that the final image will preserve transparency on every page. PhantomJS is deprecated, so use it only for a legacy workflow; for maintained automation, migrate to headless Chrome or Firefox and verify the PNG’s alpha channel on the page you actually need to capture.

Why a PhantomJS screenshot can be transparent

PhantomJS documents that it does not set a page background color itself: the page determines the background, and if the page sets none, it remains transparent. This is different from assuming that PhantomJS automatically makes every screenshot transparent. A site can paint a background through its CSS, and page content or browser compositing can also affect the result.

For an opaque white background, PhantomJS’s documented example is document.body.bgColor = 'white';. For transparency, leave the page background unset or remove the rule that paints it. Setting the body’s inline background to transparent can help when the body has a background declaration, but it cannot guarantee that every other page element or rendering layer is transparent.

Capture a transparent PNG with legacy Selenium and PhantomJS

This Python example shows the legacy pattern. It assumes a locally installed PhantomJS executable and a Selenium version that still exposes webdriver.PhantomJS. The code does not pin versions: Selenium’s support for this constructor is legacy, and the compatible combination depends on the environment you are maintaining.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium import webdriver

# Requires a locally installed PhantomJS executable and a Selenium version
# that still exposes the legacy webdriver.PhantomJS constructor.
driver = webdriver.PhantomJS(service_log_path='/tmp/phantomjs.log')
driver.set_window_size(1200, 800)
driver.get('https://example.com')

# If the page paints a body background, try removing or overriding it.
# Transparency still depends on the page and rendering behavior.
driver.execute_script("document.body.style.background = 'transparent';")
driver.save_screenshot('/tmp/example-transparent.png')
driver.quit()

The screenshot path should end in .png. Selenium’s Python API also offers get_screenshot_as_file(filename) to save a PNG and get_screenshot_as_png() to return PNG bytes. Those APIs document PNG capture, not universal preservation of an alpha channel.

Make the capture repeatable

  1. Use the legacy stack only where you must. Confirm that the installed Selenium package still exposes the PhantomJS constructor and that the PhantomJS executable is installed and available to the process.
  2. Set the viewport before navigation. The example uses 1200 by 800 pixels. Choose dimensions appropriate to the page and set them before capture so the viewport is predictable.
  3. Navigate to the target page. If content loads asynchronously, wait until the page is in the state you want before taking the screenshot. The example does not include a site-specific readiness condition because that condition depends on the target page.
  4. Check for painted backgrounds. Inspect the page’s body and relevant elements for backgrounds. The inline body style in the example is an override attempt, not a universal fix for backgrounds painted elsewhere.
  5. Save the PNG and inspect its alpha channel. Open it in an alpha-aware image editor or inspect it with an image-processing tool. A checkerboard preview commonly indicates transparent pixels; a white area can mean the page or compositor produced an opaque image.
  6. Always close the driver. The example calls driver.quit() after saving the file, which releases the browser process.

How to tell whether the PNG is really transparent

A PNG file extension alone does not prove that the image has transparent pixels. Check the image in an editor that displays transparency as a checkerboard, or inspect the alpha channel with your image tooling. Look at the pixels where you expected the page background to show through. If they are opaque, the screenshot may still be a valid PNG; it simply does not have the transparency your workflow requires.

If transparency matters for production output, make this inspection part of your own validation process. Selenium’s generic screenshot methods do not promise alpha-channel preservation, and the actual result can vary with page styling, content, compositing, and browser behavior.

PhantomJS status and what to use instead

Selenium’s change notes mark PhantomJS deprecated and recommend Chrome or Firefox in headless mode. Selenium’s JavaScript change notes also record removal of native PhantomJS support because its WebDriver implementation was no longer actively developed. Current Selenium Python APIs for Chromium and Firefox provide screenshot methods, making migration practical.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a maintained-browser screenshot in Python, the following headless Chrome sketch uses Selenium’s current browser-specific options pattern. Install and configure Chrome and its Selenium-compatible driver in your environment before running it.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless=new')
options.add_argument('--window-size=1200,800')
driver = webdriver.Chrome(options=options)
try:
    driver.get('https://example.com')
    driver.save_screenshot('/tmp/example.png')
finally:
    driver.quit()

This is a browser migration for screenshot capture, not a guarantee of transparent output. If the alpha channel is essential, test the exact target page under the browser and CI/container environment you intend to use. Firefox also has Selenium screenshot APIs, including full-page screenshot methods documented in its Python binding; select a browser based on your compatibility and automation needs, not an assumed universal transparency result.

Choosing between the legacy and maintained routes

Consideration PhantomJS with legacy Selenium Headless Chrome or Firefox with Selenium
Maintenance status Deprecated by Selenium; keep only where legacy compatibility requires it. Recommended direction in Selenium’s change notes.
Screenshot API Legacy Selenium driver can save a screenshot; check that your installed version still exposes the constructor. Current Chromium and Firefox Python bindings document screenshot APIs.
Transparency PhantomJS documents an unset page background as transparent, but page styling and rendering still matter. Do not assume alpha preservation; validate the target page’s resulting PNG.
Performance and runtime cost No general benchmark is established here for a meaningful comparison. No general benchmark is established here for a meaningful comparison.
CI and container fit Depends on the legacy executable and compatible Selenium setup in your environment. Depends on how the chosen browser and driver are installed and configured in your environment.

The available documentation supports the maintenance recommendation and API availability, but not a universal speed ranking, runtime estimate, or CI/container guarantee. Test startup, rendering, and alpha output in the environment that will run the job.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need screenshots through an API rather than managing a local browser, ScreenshotNeo accepts a URL in one GET request and can return PNG, JPEG, WebP, or PDF. Its feature set includes a transparent-background option; as with any capture, check the output against your target page and format. The service removes cookie banners, newsletter popups, and chat widgets before capture, and each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers identify the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents and other MCP clients.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

One-call cURL example (save the response as WebP):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo documentation for request options and setup. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Sign up for the free plan to try it.

Troubleshooting common failures

  • webdriver.PhantomJS is missing: The Selenium release in use no longer exposes that legacy path. For a short-term legacy system, use a compatible old setup only if you can maintain it; otherwise migrate to headless Chrome or Firefox.
  • The driver cannot start PhantomJS: The legacy example requires the PhantomJS executable to be installed and accessible to the process. Check the executable and the configured environment, and inspect the log at /tmp/phantomjs.log.
  • The image is opaque despite the CSS override: The page may paint a background on another element, or the renderer may produce opaque output. Inspect the page’s styling and the image’s alpha channel rather than treating the CSS assignment as proof of transparency.
  • The capture is blank or incomplete: The example navigates and captures immediately. If the page needs time or client-side rendering, wait for the target content or a page-specific readiness condition before saving.
  • The file is not written as expected: Ensure the destination directory exists and that the process can write there. Use a filename ending in .png for the documented Selenium PNG file method.
  • Output dimensions differ from expectation: Set the window size before navigation and capture, and inspect the resulting image dimensions in the PNG viewer or processing tool.
  • Transparency works locally but not in CI: Browser and page rendering can differ across environments. Reproduce the target in the actual CI/container environment and validate alpha there; the generic Selenium API does not guarantee identical alpha results.

FAQ

Does calling save_screenshot() make a PNG transparent?

No. It saves a PNG screenshot, but PNG format and transparency are separate properties. Verify that the captured image contains transparent pixels.

Can I make a PhantomJS screenshot opaque instead?

Yes. PhantomJS’s FAQ gives document.body.bgColor = 'white'; as an example for setting a white body background before capture.

Should a new Selenium project use PhantomJS?

No. Selenium marks PhantomJS deprecated and recommends headless Chrome or Firefox. Keep PhantomJS only when maintaining a legacy integration that depends on it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.