How to Enable Cookies in the R webshot Package
The R webshot package has no documented cookies argument. Learn what its JavaScript hook can do, what it cannot, and how to troubleshoot cookie-dependent captures.

webshot::webshot() has no documented cookies argument in the webshot 0.5.5 reference manual. Its documented eval option can run JavaScript in the page after it opens and after the configured delay, but that is not a browser cookie jar and does not establish a logged-in session before navigation. If a site needs a cookie to show the right content, test the actual site and its supported login flow; the package documentation does not guarantee a general cookie-injection workaround.
The practical choice is to use eval for page-side state that can safely be set after the page has loaded, or to use a browser automation approach with an explicitly documented pre-navigation cookie API if the site requires one. Switching to webshot2 gives you a Chrome-based backend, but its documented call also has no cookies parameter. If you need a screenshot without managing a browser runtime, ScreenshotNeo offers a one-request screenshot API; its cookie, banner-cleanup, and billing behavior is described below.
1. What “enable cookies” means in webshot
People use “enable cookies” to mean two different things. A browser may have cookies enabled as a capability, while an individual capture may need a particular cookie value to choose a consent state, identify a session, or unlock protected content. The first is a browser setting; the second is state that must be available to the page at the right time.
The documented webshot() arguments do not expose a cookie collection, cookie file, or browser-context cookie setter. The manual documents eval as a JavaScript string evaluated after the page opens and the configured delay elapses, before the clipping region is calculated and the screenshot is taken. The manual’s example uses a Casper callback to interact with page elements. That establishes a page-side JavaScript hook, not a general mechanism to set browser cookies before navigation.
This timing is decisive. A server often reads a cookie while returning the initial HTML. Code that runs after the page opens cannot retroactively change that response. In some sites, JavaScript can set a non-HttpOnly cookie or local storage value and then the page can update itself. In others, the value is rejected, inaccessible to JavaScript, scoped to a different host or path, or only useful before the first request.
2. Install and run the documented page-side hook
The original webshot package uses PhantomJS, an external browser executable. The package introduction points to webshot::install_phantomjs() or manual installation with the executable available on PATH. This does not add a cookie argument, but it is a prerequisite for running the package.
- Install the R package and its PhantomJS runtime.
- Confirm that the URL loads in the target environment, including any network or authentication prerequisites.
- Use
evalonly for page-side code that is appropriate after load; set a delay if the page needs time to update. - Inspect the resulting image and verify the relevant content, not merely that a PNG file was written.
install.packages("webshot")
webshot::install_phantomjs()
webshot::webshot(
url = "https://example.com",
file = "capture.png",
delay = 1,
eval = "document.cookie = 'site_consent=accepted; path=/';"
)
This code demonstrates the documented hook and a browser-side cookie assignment. It is not a promise that example.com, or any other target, will accept the cookie or render a particular state. Use a cookie name and value supported by the site, and do not put real session secrets into code that may be committed, logged, or shared.
When this technique can work
A client-side cookie assignment may be useful when the page itself checks that cookie after loading and updates its display. The cookie must be valid for the current origin and path, and the site must permit JavaScript to set it. If the site reads the cookie only on the initial server request, setting it during eval is too late for that request. You may need a reload, but the documented hook alone does not turn into a supported pre-navigation cookie API simply by adding reload JavaScript.
A value marked HttpOnly cannot be set or read through document.cookie. Secure, domain, path, and SameSite rules can also affect whether a browser sends a cookie in a particular context. The package documentation does not describe these site-specific rules or promise that eval can overcome them. For authenticated captures, use the site’s supported login or session flow and test that exact target.
3. Understand the limits of JavaScript and page state
eval runs in the page after opening; it is not a way to seed a browser profile before the first request. Keep these cases distinct:

| Need | What the documented hook tells you | Practical implication |
|---|---|---|
| Set a client-readable cookie after load | Page-side JavaScript can be evaluated. | May work if the site reads it client-side and accepts its scope and value. |
| Send a cookie with the first HTTP request | eval runs after opening. |
The hook is too late to guarantee the initial request contains it. |
| Set an HttpOnly session cookie | Page JavaScript cannot set HttpOnly cookies. | Use a supported authentication flow or a tool with a verified browser-context cookie API. |
| Dismiss a consent banner | JavaScript may interact with page elements. | A site may store consent state, but the selector and state mechanism are site-specific. |
| Reuse state across captures | The cited manual does not document a cookie jar or persistence contract. | Do not assume one call shares cookies with another. |
A cookie can also be the wrong lever. A consent dialog might store state in local storage, use a vendor-specific consent platform, or require a button click that triggers additional requests. Login may depend on redirects, CSRF tokens, or multiple cookies. Avoid copying a cookie string from a live browser session into a script unless you understand its scope, expiry, and handling requirements.
4. Choose between webshot and webshot2
The original webshot uses PhantomJS. The official webshot2 repository describes it as a replacement that uses headless Chrome through Chromote. It requires Chrome or another Chromium-based browser. The inspected documented webshot2 call also does not include a cookies argument, so changing packages is a browser-backend change, not a documented cookie solution.

| Package | Browser backend | Runtime requirement | Documented cookie argument |
|---|---|---|---|
webshot |
PhantomJS | Install PhantomJS or make its executable discoverable on PATH. |
None in the reference manual; eval is available. |
webshot2 |
Headless Chrome via Chromote | Chrome or a Chromium-based browser. | None in the inspected function documentation. |
For an existing script, retain webshot if its supported behavior meets the need and the PhantomJS runtime is available. Consider webshot2 when you need its Chrome-based rendering path, but verify the package’s current documentation and your installed version. Neither package’s documented API should be described as a general cookie injection API based on these references.
5. Configure capture timing and output deliberately
The cookie question is often mixed up with timing. The delay argument gives page activity time to settle before capture and before the documented eval hook is evaluated. A longer delay can help with an animation or a client-side update, but it does not guarantee authentication, cause the server to repeat its original request with a newly set cookie, or prove that all dynamic content is ready.
For repeatable captures, keep the target URL and output path explicit, set only the delay needed by the page, and make the output format intentional through the filename extension. If you use eval, keep it small and specific: a broad script that clicks arbitrary buttons can accept terms, submit forms, or navigate away. Use a stable selector only after checking the target page’s current structure.
For a protected page, first establish whether the problem is server-side access or client-side rendering. If a normal browser without an existing session receives a login page, a post-load script cannot make the initial request authenticated. If the response already contains the desired page but a client-side consent overlay obscures it, page-side interaction may be relevant. These are different failure modes and need different fixes.
6. Troubleshooting common failures
| Symptom | Likely cause | What to check or change |
|---|---|---|
unused argument (cookies = ...) |
The function does not document a cookies parameter. |
Remove it. Use the documented eval hook only for page-side work, or choose a tool whose current documentation explicitly supports pre-navigation cookies. |
| The screenshot still shows a login page | The target requires a session on the initial request, or its login flow is not available to page-side code. | Verify access in a clean browser session. Use the site’s supported authentication flow and test the exact destination. Do not assume a cookie assignment after load can authenticate the original request. |
document.cookie appears ineffective |
The cookie may be HttpOnly, scoped to another host/path, rejected by browser rules, or not the state the site uses. | Confirm the site’s expected cookie behavior. Check whether it uses local storage or a consent platform. Avoid treating client-side assignment as a universal fix. |
| Consent banner remains visible | Consent may require a specific button interaction or another storage mechanism; the script may run before the relevant element exists. | Check the page structure and timing. Use a suitable delay and a narrowly targeted interaction if allowed. Verify the resulting state manually. |
| Capture is blank or incomplete | The page may still be loading, blocked, redirected, or dependent on scripts and resources. | Open the same URL from the capture environment. Adjust timing only when the page needs it; investigate access and resource failures separately. |
| PhantomJS executable not found | The external runtime is absent or not discoverable. | Use webshot::install_phantomjs() or manually install PhantomJS and make it available on PATH, as described in the package documentation. |
| Migration to webshot2 still does not set cookies | The Chrome backend does not imply a cookie argument in the documented wrapper. | Check the installed webshot2 documentation and use only documented parameters. Backend modernization is not proof of cookie support. |
| Works locally but not on a server | The server may lack the browser runtime, network access, fonts, or the same authentication state. | Check runtime installation and URL access from that environment. Treat local browser state as separate from server state. |
7. Reliability, performance, and cost considerations
For a local package workflow, the principal operational dependency is the browser runtime: PhantomJS for webshot, or Chrome/Chromium for webshot2. Pin the R package versions used by an application and verify runtime availability in the deployment environment. Browser rendering can vary with the page’s scripts, resources, and timing, so a completed function call alone does not establish that the intended authenticated or consent state appeared.
Use the shortest delay that reliably allows the page to reach the state you need. Arbitrarily large waits make batch work slower; arbitrarily short waits can capture a loading state. If an operation involves credentials, keep secrets outside source control and restrict access to output files. Session cookies are credentials in practice, even when represented as plain text.
Package-based captures have no per-capture ScreenshotNeo charge, but you maintain the runtime and execution environment yourself. If you choose a hosted screenshot service, compare its request behavior, response semantics, features, and plan limits for your workload. Do not infer costs or reliability from a browser backend alone.
8. Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server from Yorker Media. Make one GET request to capture a URL; the API can return PNG, JPEG, WebP, or PDF. See the ScreenshotNeo API documentation for request options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
ScreenshotNeo accepts cookie and Authorization parameters, and it removes known consent banners, newsletter popups, and chat widgets before capture; each of those cleanup steps can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed. The response identifies the page verdict and billing status in X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.
The free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 screenshots; the stated tiers are Starter $5/3,000, Growth $15/15,000, Pro $39/60,000, Scale $99/250,000, and Business $249/1,000,000. Yearly billing gives two months free, and every feature is on every plan. Sign up for 1,000 free screenshots a month with no card.
9. FAQ
How do I use cookies with webshot in R?
There is no documented direct cookies argument in the webshot 0.5.5 reference manual. Use eval only for suitable page-side JavaScript after load, and verify the result against the target site.
Does eval set a cookie before the page loads?
No. The documented hook runs after the page opens and its configured delay elapses. It cannot guarantee that the initial HTTP request carries a cookie.
Does webshot2 solve cookie injection?
Its Chrome and Chromote backend differs from PhantomJS, but the inspected documented call has no cookies parameter. Confirm current API documentation before relying on any newer capability.
Can JavaScript set an authenticated session cookie?
Not if it is HttpOnly, and setting a client-readable value still does not prove that the server or site will accept it. Use the site’s supported session flow and test that exact page.
Sources and version note
The package details above are based on the webshot 0.5.5 reference manual, the webshot package introduction, the PhantomJS installation documentation, and the official webshot2 repository and inspected function documentation. Package APIs can change; check the documentation for the versions installed in your environment. The cited documentation does not establish that the example cookie assignment works on a particular site.


