<?xml version="1.0" encoding="UTF-8"?>
<response><data><post><id>88</id><slug>working-with-csrf-tokens-part-5-88</slug><title>Working with CSRF tokens, part 5</title><excerpt>In part 5 we take the ideas from earlier instalments and apply them to a larger, messier site. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to…</excerpt><seriesName>Working with CSRF tokens</seriesName><seriesPart>5</seriesPart><sections><item><heading>Introduction</heading><paragraphs><item>In part 5 we take the ideas from earlier instalments and apply them to a larger, messier site. 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>Going further</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>Store raw responses alongside parsed output while you are developing. When a parser breaks, you can replay the saved HTML instead of hitting the site again.</item><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></paragraphs></item><item><heading>Putting it into practice</heading><paragraphs><item>Pagination comes in several shapes: numbered pages, next links, offsets and opaque cursors. Each has trade-offs, and a robust scraper should detect the end of the result set rather than assuming a fixed number of pages.</item><item>Character encodings still cause trouble. Decode responses using the charset the server declares, and normalise text before comparing or storing it.</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>5</authorId><authorName>Chen Patel</authorName><authorSlug>chen-patel</authorSlug><category>Testing</category><tags><item>selenium</item><item>testing</item><item>json</item><item>playwright</item></tags><publishedAt>2026-02-27T00:00:00.000Z</publishedAt><wordCount>220</wordCount><readingTimeMinutes>2</readingTimeMinutes></post></data></response>
