Skip to content
ホーム

/

用語集

/

ブラウザ

/

CSS Selector

ブラウザ

3分で読了

CSS Selectorとは?

A CSS selector is a pattern used to identify and target specific HTML elements on a web page, widely used in browser automation to locate buttons, forms, text, and other interactive elements.

What is a CSS Selector?

A CSS selector is a string pattern that identifies specific HTML elements within a web page's Document Object Model (DOM). Originally designed for styling web pages with CSS stylesheets, selectors have become the primary mechanism for locating elements in browser automation, web scraping, and testing frameworks.

Types of CSS Selectors

CSS selectors range from simple to highly specific:

  • Element selectors: Target elements by tag name (e.g., div, button, input)
  • Class selectors: Target elements by class attribute (e.g., .product-card, .btn-primary)
  • ID selectors: Target a unique element by its ID (e.g., #login-form, #submit-btn)
  • Attribute selectors: Match elements based on attribute values (e.g., [data-testid="price"], input[type="email"])
  • Combinators: Express relationships between elements — descendant (div p), child (ul > li), sibling (h2 + p)
  • Pseudo-classes: Filter by state or position (e.g., :first-child, :nth-of-type(2), :not(.hidden))
  • CSS Selectors in Browser Automation

    In automation frameworks like Playwright and Selenium, CSS selectors are the most common way to locate elements for interaction. A well-crafted selector is:

  • Specific enough to match exactly the intended element
  • Resilient to minor DOM changes like reordered attributes or added wrapper elements
  • Readable so that other team members can understand what element is being targeted
  • Best practices for automation selectors:

  • Prefer data-testid or data-* attributes over class names, which change frequently with CSS refactors
  • Avoid overly deep descendant chains like div > div > div > span that break when layouts change
  • Use attribute selectors for stable identifiers like [role="navigation"] or [aria-label="Search"]
  • CSS Selectors vs. XPath

    Both CSS selectors and XPath can locate DOM elements, but they have different strengths. CSS selectors are generally shorter, more readable, and faster to evaluate. XPath supports traversing up the DOM tree (selecting parent elements) and more complex conditional logic, which CSS cannot express. Most modern automation tools support both.

    なぜ重要か

    CSS selectors are the foundation of reliable browser automation. Poorly chosen selectors are the number one cause of flaky automation scripts — they break every time a website updates its design. Understanding selector strategy is essential for building maintainable automations.

    Autonolyのソリューション

    Autonoly's AI agent automatically identifies the most robust CSS selectors for each element it interacts with. Through its Site Knowledge system, Autonoly remembers which selectors have worked reliably across sessions and warns when a previously successful selector starts failing, continuously improving selector resilience.

    詳しく見る

    • Using `button[data-action='add-to-cart']` to reliably click an Add to Cart button across site redesigns

    • Targeting `.product-list > .product-item` to extract all product cards from a listing page

    • Selecting `input[name='email']` to fill in an email field during automated form submission

    よくある質問

    CSS selectors use a concise syntax designed for styling and are generally faster to evaluate. XPath uses a path-based syntax that can navigate up the DOM tree (to parent or ancestor elements) and supports more complex conditional expressions. For most browser automation tasks, CSS selectors are preferred for their readability and performance.

    Focus on stable attributes like data-testid, name, role, or aria-label rather than class names or deeply nested element paths. Avoid relying on positional selectors like :nth-child unless the page structure is guaranteed to be stable. Test selectors against multiple states of the page to ensure they remain unique.

    自動化について読むのはここまで。

    自動化を始めましょう。

    必要なことを日本語で説明するだけ。AutonolyのAIエージェントが自動化を構築・実行します。コード不要。

    機能を見る