|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: selector |
| 4 | +permalink: /parameters/selector/ |
| 5 | +parent: Parameters |
| 6 | +nav_order: 9 |
| 7 | +description: >- |
| 8 | + Learn how to select and crop specific elements from a webpage using CSS selectors. |
| 9 | +--- |
| 10 | + |
| 11 | +# CSS Selector Parameter |
| 12 | +{: .no_toc } |
| 13 | +{: .fs-9 } |
| 14 | + |
| 15 | +Precisely capture specific elements from your webpage |
| 16 | +{: .fs-6 .fw-300 } |
| 17 | + |
| 18 | +<hr> |
| 19 | + |
| 20 | +Table of contents |
| 21 | +{: .text-delta } |
| 22 | +- TOC |
| 23 | +{:toc} |
| 24 | + |
| 25 | +<hr> |
| 26 | + |
| 27 | +## Overview |
| 28 | + |
| 29 | +The `selector` parameter allows you to capture specific elements from a webpage by providing a CSS selector. The API will automatically crop the image to match the dimensions of the selected element. |
| 30 | + |
| 31 | +{% include hint.md title="Works Everywhere" text="The selector parameter works with both HTML snippets and full URL screenshots." %} |
| 32 | + |
| 33 | +## Basic Usage |
| 34 | + |
| 35 | +### HTML Example |
| 36 | + |
| 37 | +Consider this HTML structure: |
| 38 | + |
| 39 | +```html |
| 40 | +<div class="margin"> |
| 41 | + <div class="content"> |
| 42 | + This is an example |
| 43 | + </div> |
| 44 | +</div> |
| 45 | +``` |
| 46 | + |
| 47 | +To capture only the inner content div, set the selector parameter: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "html": "<div class=\"margin\">...</div>", |
| 52 | + "selector": ".content" |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +{% cloudinary /assets/images/selector-example.png alt="Example of using a CSS selector to crop an image" %} |
| 57 | + |
| 58 | +## Common Selector Types |
| 59 | + |
| 60 | +### Class Selectors |
| 61 | + |
| 62 | +To select elements by class name: |
| 63 | + |
| 64 | +```html |
| 65 | +<!-- HTML --> |
| 66 | +<div class="profile-card">...</div> |
| 67 | + |
| 68 | +<!-- Selector --> |
| 69 | +.profile-card |
| 70 | +``` |
| 71 | + |
| 72 | +### ID Selectors |
| 73 | + |
| 74 | +To select a unique element by ID: |
| 75 | + |
| 76 | +```html |
| 77 | +<!-- HTML --> |
| 78 | +<div id="header">...</div> |
| 79 | + |
| 80 | +<!-- Selector --> |
| 81 | +#header |
| 82 | +``` |
| 83 | + |
| 84 | +### Nested Selectors |
| 85 | + |
| 86 | +To select nested elements: |
| 87 | + |
| 88 | +```html |
| 89 | +<!-- HTML --> |
| 90 | +<div class="container"> |
| 91 | + <section class="content"> |
| 92 | + <article class="post">...</article> |
| 93 | + </section> |
| 94 | +</div> |
| 95 | + |
| 96 | +<!-- Selector --> |
| 97 | +.container .content .post |
| 98 | +``` |
| 99 | + |
| 100 | +### Multiple Classes |
| 101 | + |
| 102 | +To select elements with multiple classes: |
| 103 | + |
| 104 | +```html |
| 105 | +<!-- HTML --> |
| 106 | +<div class="card premium featured">...</div> |
| 107 | + |
| 108 | +<!-- Selector --> |
| 109 | +.card.premium.featured |
| 110 | +``` |
| 111 | + |
| 112 | +## Advanced Selectors |
| 113 | + |
| 114 | +For more complex selections, you can use: |
| 115 | + |
| 116 | +| Selector | Example | Description | |
| 117 | +|:---------|:--------|:------------| |
| 118 | +| Child | `parent > child` | Selects direct children only | |
| 119 | +| Nth Child | `div:nth-child(2)` | Selects specific child elements | |
| 120 | +| Attribute | `[data-type="premium"]` | Selects elements with specific attributes | |
| 121 | +| Combinators | `header + .content` | Selects elements that follow others | |
| 122 | + |
| 123 | +## Best Practices |
| 124 | + |
| 125 | +1. **Be Specific**: Use precise selectors to ensure you capture exactly what you need |
| 126 | +2. **Test First**: Verify your selector works in the browser before using it in the API |
| 127 | +3. **Consider Dynamic Content**: Allow time for JavaScript-rendered content using `ms_delay` if needed |
| 128 | +4. **Unique Identifiers**: When possible, use IDs for more reliable selection |
| 129 | + |
| 130 | +{% include hint.md title="Performance Tip" text="More specific selectors can help reduce processing time as the API can find the element faster." %} |
| 131 | + |
| 132 | +## Common Issues and Solutions |
| 133 | + |
| 134 | +### Element Not Found |
| 135 | +If your selector doesn't match any elements, try: |
| 136 | +- Verifying the selector syntax |
| 137 | +- Ensuring dynamic content has loaded (use `ms_delay`) |
| 138 | +- Checking for typos in class/ID names |
| 139 | + |
| 140 | +### Wrong Element Selected |
| 141 | +If the wrong element is captured: |
| 142 | +- Make your selector more specific |
| 143 | +- Use unique identifiers when possible |
| 144 | +- Check for duplicats |
0 commit comments