{"data":{"id":28,"slug":"working-with-csrf-tokens-part-2-28","title":"Working with CSRF tokens, part 2","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…","seriesName":"Working with CSRF tokens","seriesPart":2,"sections":[{"heading":"Introduction","paragraphs":["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."]},{"heading":"Step by step","paragraphs":["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.","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."]},{"heading":"Common pitfalls","paragraphs":["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.","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."]},{"heading":"Key takeaway","paragraphs":["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."]}],"authorId":9,"authorName":"Mateo Hughes","authorSlug":"mateo-hughes","category":"Testing","tags":["playwright","http","python"],"publishedAt":"2026-07-12T00:00:00.000Z","wordCount":202,"readingTimeMinutes":2}}