<?xml version="1.0" encoding="UTF-8"?>
<response><data><post><id>28</id><slug>working-with-csrf-tokens-part-2-28</slug><title>Working with CSRF tokens, part 2</title><excerpt>Part 2 looks at what changes when the same technique has to run every day in production. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to a…</excerpt><seriesName>Working with CSRF tokens</seriesName><seriesPart>2</seriesPart><sections><item><heading>Introduction</heading><paragraphs><item>Part 2 looks at what changes when the same technique has to run every day in production. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to a site: the form posts fine in a browser but fails with a 403 from code.</item></paragraphs></item><item><heading>Step by step</heading><paragraphs><item>Selectors are the most fragile part of any scraper. Prefer stable attributes such as data-testid or semantic structure over generated class names, which can change on every deploy.</item><item>Dynamic pages render content after the initial HTML arrives. A browser automation tool can wait for a specific element to appear, which is far more reliable than sleeping for a fixed amount of time.</item></paragraphs></item><item><heading>Common pitfalls</heading><paragraphs><item>Read the site's terms of service before you start. Many sites welcome automated access within limits and say so explicitly; others ask you to use an API instead.</item><item>Log every request with its URL, status code and duration. When something goes wrong at two in the morning, those logs are the difference between a quick fix and a long investigation.</item></paragraphs></item><item><heading>Key takeaway</heading><paragraphs><item>Load the form page with a session that keeps cookies, read the hidden token field, and send it back with the same session in the same request.</item></paragraphs></item></sections><authorId>9</authorId><authorName>Mateo Hughes</authorName><authorSlug>mateo-hughes</authorSlug><category>Testing</category><tags><item>playwright</item><item>http</item><item>python</item></tags><publishedAt>2026-07-12T00:00:00.000Z</publishedAt><wordCount>202</wordCount><readingTimeMinutes>2</readingTimeMinutes></post></data></response>
