<?xml version="1.0" encoding="UTF-8"?>
<response><data><post><id>170</id><slug>choosing-css-selectors-that-survive-redesigns-part-9-170</slug><title>Choosing CSS selectors that survive redesigns, part 9</title><excerpt>For part 9, we revisit the basics with a fresh example and a few lessons learned the hard way. A selector that worked yesterday and fails today is usually a selector that depended on something nobody…</excerpt><seriesName>Choosing CSS selectors that survive redesigns</seriesName><seriesPart>9</seriesPart><sections><item><heading>Introduction</heading><paragraphs><item>For part 9, we revisit the basics with a fresh example and a few lessons learned the hard way. A selector that worked yesterday and fails today is usually a selector that depended on something nobody promised to keep the same, such as a generated class name or the position of a div.</item></paragraphs></item><item><heading>A practical approach</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>Keep configuration such as base URLs, delays and selectors out of your code. Changing a delay should not require a redeploy.</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>Step by step</heading><paragraphs><item>Validate the data you extract. Prices without currencies, dates without time zones and empty fields are common sources of silent errors downstream.</item><item>Write small, focused tests for your parsers using saved HTML fixtures. They run in milliseconds and catch layout changes long before a full crawl would.</item></paragraphs></item><item><heading>Key takeaway</heading><paragraphs><item>Prefer data-testid attributes, ARIA roles and visible labels. Avoid long chains of nth-child and anything that looks like a hash.</item></paragraphs></item></sections><authorId>4</authorId><authorName>Lucas Silva</authorName><authorSlug>lucas-silva</authorSlug><category>Testing</category><tags><item>testing</item><item>json</item><item>ethics</item><item>scrapy</item></tags><publishedAt>2024-06-27T00:00:00.000Z</publishedAt><wordCount>203</wordCount><readingTimeMinutes>2</readingTimeMinutes></post></data></response>
