Skip to main content

Selecting the right element

Use CSS selectors to ensure the right element (including dynamic) on the page is selected for your Experience to appear or trigger correctly

Written by Chameleon Team

One of the most powerful abilities of Chameleon is the possibility to anchor in-app Experiences or trigger Steps based on on-page elements. As every application is unique, you can further adjust selectors to ensure your Experiences display correctly.

All to deliver Experiences that are contextual and guide your users to discover clear moments of value in your product.

🧑‍💻 Ask your Developer to help: element selection can get tricky, but a developer on your team can help out by adding an ID (or an attribute) to your elements and help make sure your Experiences can reliably use it for positioning or targeting. You can also contact us if you get stuck, and we can take a look!


How are elements used in Chameleon?

Elements are essentially different boxes of content on a page defined by HTML and CSS code. Chameleon uses on-page elements to establish where and when an Experience should show up, along your defined user Segments, or set triggers for example.

By leveraging the presence or absence of elements, you can show users specific parts of your interface and help them discover more features. 😉

Here are a few examples of how you can leverage elements in Chameleon:

  • require an element is present or clicked to show a Step;

  • position a Tour Step or Embeddable by snapping it to an element on the page;

  • highlight a particular element;

  • fire a click on an element when a user clicks the "Next" button in a Step;

  • require an element to be clicked to proceed to the next Step of the Tour;

  • track clicks and use them for targeting Tours; and more.


All these cases require Chameleon to correctly identify and save the relevant element on the page. We are built to handle this, but it's useful to know how it works, in case you need to make a manual adjustment 😊


How to select an element in Chameleon

Whenever there is the opportunity to Select an element, you will see the same point-and-click flow. Hover your mouse over your page to identify the element your want to leverage.

ℹ️ After you click an element, you may briefly see "Building a stable selector…" while Chameleon removes names your app generates automatically, which can change on every deploy. If this takes longer than about 20 seconds, Chameleon keeps the element exactly as you picked it. Read Why did my element selection stop working? for more

The available elements will have a green outline appear around and you can simply click to select one. Click "Confirm" in the Builder panel that opens up and your Step will be anchored or conditioned to be shown by this element.


See how reliable your selection is

Right after you click an element, Chameleon checks how likely the selection is to keep working. If it matches only that element and none of the risks below apply, you'll see a Reliable badge and the selection saves on its own. Otherwise the Adjust element panel stays open and explains what it found, so you can fix it before you press Confirm.

If Chameleon couldn't check the selection for names your site generates automatically (for example, a selection saved earlier and opened again), you'll see "Not checked for names your site generates automatically, so it isn't marked Reliable." You can still save it as it is.

When the check finds a risk, it explains it in plain words. These don't stop you from saving; they're advice for keeping the selection working after your next release:

Risk

What you'll see

Relies on position

"Picks this by its position. If items are added or reordered, it will point at a different element."

Relies on text

"Picks this by its text. Copy changes and translations will break it."

Relies on where it sits in the page

"Depends on where this sits in the page. A layout change will break it."

No stable handle

"This element has no id, testing attribute or label to anchor on."

Auto-generated names

"Uses names your site generates automatically. These can change on your next update."

Inside a web component

"This sits inside a web component, which limits how reliably it can be matched."

These problems stop you from saving the selection as it is, and the panel says why:

What happened

What you'll see

Matches nothing, right after you pick it

"Matches nothing on this page, so it can't be saved as it is. Pick the element again, or adjust the selection until it matches."

Matches several elements, and you haven't chosen which one to use

"Matches [number] elements, so it can't be saved as it is: Chameleon would use whichever comes first. Narrow it down until it matches one." Where you can pick one of the matches, it adds "Or choose which match to use below."

Matches more than 200 elements

"Matches more than 200 elements, so it can't be saved. Chameleon only looks at the first 200 — narrow it down until it matches one."

A CSS selector you typed isn't valid

"This selector isn't valid, so it can't match anything or be saved."

Ask Copilot to suggest a sturdier element

Whenever a selection isn't marked Reliable, the panel shows an Ask Copilot button. Clicking it opens Copilot already holding your selection and what the check found, so you don't have to describe the problem yourself.

Copilot explains each problem in a sentence or two, then looks at the elements around the one you picked (up to four levels of parent elements, plus its children and their children) for one that matches only itself, has an id, testing attribute or label to anchor on, and removes a risk without adding one. If it finds one, it outlines it on your page and asks whether to use it; nothing changes until you say yes. Then it puts that element into your selection, and you still press Confirm in the picker to save it.

If nothing nearby is sturdier, Copilot says so rather than suggesting a weaker element, and recommends asking your developers to add a stable testing attribute (like data-testid) to the element you want.

⚠️ Applying Copilot's suggestion needs a role that can edit Experiences (Admin, Publisher or Creator). Without one, Copilot can still explain the check and outline a sturdier element, but it tells you the change wasn't applied and suggests asking your account admin to upgrade your role.

CSS selectors are key

Using the point-and-click editor you can select any element that is present on your page. Once you select one, Chameleon will know to condition your Step to the presence, absence, or unique content of this element.

Chameleon identifies the element you have selected using CSS Selectors: these are code identifiers that target an HTML component on the page.

In the image above, "body" is the CSS Selector, but there are many more you may encounter. Some of the most common include the element selector (e.g. <div>, <p> etc.), <class> and <id>. Together these can help uniquely identify an element on the page.

If you find that your element isn't correctly identified, you can adjust it further to fit the exact one you want to target. Read more about selecting and adjusting your elements below👇


How can you adjust selected elements?

In some cases, you may not see the correct element highlighted (with the green outline) or the match may be incorrect if you return later. So, it might be necessary to manually Adjust the element to accurately match the intended one.

The Adjust element panel will give you three broad ways to input the correct selectors:

  1. Auto identification

  2. Specify your element further via single selectors

  3. Add custom CSS path

👉 You can also include here any data attributes to pinpoint the right element. This is especially useful if your product has dynamic classes, as you can include a unique attribute to help Chameleon identify specific elements when building out Experiences.

As you adjust the CSS Selectors, the matching will be updated in real-time and the matching element will show the green outline. You can use this as a guide to check whether you've made the correct adjustments to match the intended element.

1. Auto identification

Chameleon will detect the different elements on the page and select the one you point to. If your element selection contains multiple matches (for example, you may be struggling to specify a specific menu item in a list of menu options), you can use this option to specify which element in the hierarchy to select.

With the "Auto identification" option you can use your arrow keys to select the correct element. Use your keyboard's arrow keys and watch the green outline move from element to element on your screen until it lands on your intended selection.

ℹ For example, if your selection is to narrow you can use the arrow keys to go up/down a level or left (previous element), right (next element). Once you select the right element simply confirm.

2. Specify your element further

This option allows you to easily edit or add an individual selector to help you define the right element.

For example, you may want to use HTML Hierarchy and you can check the box, or you may want to change the 'text' value or add an additional 'id'. You can do all that easily here.

The available options to adjust how this element is selected are:

  • HTML hierarchy: all of html tags from this element up to the <body> tag. This helps us locate the element on the page and typically consists of <div> tags and their classes.

  • tag: the type of html tag associated with this specific element, such as a, input or h1

  • text: the text string this element contains, such as "Sales by day"

  • id: the unique ID, such as "chart-overview-1234"

  • class: any class names, such as "chart-main"

  • href: any link associated, such as "https://www.chameleon.io"

  • title: the title, such as "Chart"

  • type: the type, typically found on <button> and <input> fields, such "text" or "email"

  • name: the name, such as "Top sales"

  • data: any data- attribute on the element. Click the caret next to the key field to pick one of the element's own attributes, with chmln listed first. With the key set to chmln, the caret next to the value field suggests values already used on the page or saved in your account

  • aria label: the accessible label for an icon or button with no visible text, such as "Close"

  • aria labelledby: the id of another element on the page whose text labels this one

ℹ️ aria label and aria labelledby can match the exact value or just part of it. A new selection matches part of the value by default, so a small change to the label text is less likely to break it.

ℹ Because a new selection keeps only the levels nearest your element, a wrapper added, removed, or reordered further up the page won't break it. If the structure right around your element is dynamic and changes on every deploy, the hierarchy can still break: uncheck HTML hierarchy and identify the element another way, such as a data attribute.

👉 We recommend you add data-chmln to the specific element you're trying to leverage and fill in the data value based on the element you want to target.

In the example below 👇 we want to add an attribute to the 'New note' element, so we add chmln in the data field and new-button in the value field.

💡 You can apply this to any element you want to target, including your inline Launcher widgets which you can use to launch further guidance or Actions.

In this case, you would use the same principle, but update your value to match your element: data-chmln="help-launcher"

When an element has data-chmln, a new selection matches on it first, ahead of the element's id and of test attributes such as data-testid, so a Step keeps finding the element when its id changes. Give each element on a page its own value: if two share one, Chameleon falls back on the element's position or its id to tell them apart. Selections you've already made keep matching as before.

3. Adding a custom CSS path

When you're positioning a Step on the page, the element selector is used to click on a particular element and place your Experience next to that element. This feature should work in 99% of the cases that you'll run into.


You can also fully edit the CSS path to identify the correct element. This is great for a few use cases:

  • you need need a unique or very custom configuration for your selector

  • your website structure changes frequently, and you have a very specific CSS class or ID that can be used to identify the element

  • the target element is not the top element, and you're not able to select the target element

To use this, simply select the Add custom CSS path option and update (or add) your desired CSS code here.

If you're having difficulty getting the CSS Path for your element, you can follow these instructions to copy a CSS Path to that element. You may need to make some adjustments.

  1. Open up a Firefox browser and go to the page in your app where you're adding the Experience

  2. Open up the Developer Tools ( on Mac hit cmd + option + I ) and click the Select Element icon in the top left corner, which looks like a mouse arrow

  3. Select your target element, which will likely be the same container element you found using our Element Selector

  4. Within the Inspector hover over the nearby elements until you highlight your desired element on the page

  5. Right-click ( ctrl-click ) on that element in the Inspector and select Copy > CSS Path

  6. Go back to the Builder within your Chrome browser, and paste that into the Add custom CSS Path section. You may need to modify this path, for example,

    • if it's not finding any elements that match, start from the beginning of the CSS Path and delete the first elements one at a time. Select everything up to the first space ( " " ) and delete, then keep deleting until you see that it's selected your element.

    • if it's finding multiple elements that match, go back and see if there's anything else identifying about your element. For example, if it's an image, there may be some alt text you can use to specify.


Selecting multiple elements

It's not currently possible to select multiple elements together for highlighting or for other use cases. However, in some cases, you can generalize the element selection so that it matches multiple elements, and any of these individually can be used.

💡 You can generalize by adjusting the selectors (e.g. removing specifying criteria such as text or href of any element) once the element has been selected.

In the following cases, when multiple elements match, the first matching element will be used.

  • "Require element on page" (learn how this works here)

  • Show Step "On hover" or "On click" of an "Icon" / "Label" / "Custom icon"

  • Position the Step "Anchor to Trigger element" (if "Icon" / "Label" / "Custom icon" used)

  • Position the Step "Anchor to other element"

  • Defined events (used for targeting or conversion tracking)

  • Button action "Click element"

This is because when the icon/label/custom icon or Step needs to display immediately upon page load, a fixed element is required.

However in the following cases, when multiple elements match, you can choose to use only the first or any elements that match:

  • Show Step "On hover" or "On click" of an "Existing element"

  • Position the Step "Anchor to Trigger element" (if "Existing element" used)

  • Complete Experience by clicking "Specific element"

You can use this to allow a user to click/hover on any one of many elements before the Step appears, adjacent to that element, or by allowing them to click any one of a number of elements to advance from the current Step.

Selecting the last element matching

If you have several elements on the page that are matching, and you want to select the last one you can modify the custom CSS as described below.

  1. Select the element on the page

  2. Go to the "Add custom CSS" section

  3. Delete the last part of the CSS path until it matches all of the relevant elements on the page

    • For example, if several buttons all had the same text you would remove all the custom CSS up to the text on those buttons or the class name for those buttons

  4. Add in ":last" (without the quotes) to the end of that path

🤝 As always, be sure to test it and make sure that it works live as you expect it to.

Example use: let's say this was the Custom CSS that showed up when you select one item in a menu. "Components" is the first match, but we want "Tables":

body > div[class*="primary__wrapper-sc-"] > ul.navbar-nav.bg-gradient-primary.sidebar.sidebar-dark.accordion > li.nav-item > a.nav-link.collapsed[href="#"]

You would then click the backspace button on this until multiple items are selected. In this case, we delete ".nav-link.collapsed[href="#"]" off the end:

body > div[class*="primary__wrapper-sc-"] > ul.navbar-nav.bg-gradient-primary.sidebar.sidebar-dark.accordion > li.nav-item > a

👉 You can use the nth option here as well, and specify the correct number for the element you want to select.

Now we'll add in "a:last" to only select the last element, "Tables":

body > div[class*="primary__wrapper-sc-"] > ul.navbar-nav.bg-gradient-primary.sidebar.sidebar-dark.accordion > li.nav-item > a:last

And you'll notice Chameleon identifying the "Tables" element correctly now.


Finding the selectors for an element

To uncover what CSS selectors are used for a particular element, you can "Inspect" it after right-clicking on that element. The HTML content and related CSS Selectors are then shown in the browser's console, as below.

💡The value fields use a 'contains' match -- so you can use a subset of the actual value of the field to help identify it.


How to select dynamic elements?

A dynamic element has attributes that keep changing with every page refresh, like pages that display content based on different visitors. Chameleon works well with dynamically changing elements too and you can leverage them just as static elements when positioning your Experiences or conditioning their display.

In some cases, selector values (e.g. for class name) change each time the application is deployed. This means that the selector values are not persistent, and so the element selection may not continue to match. If this happens for your application (relevant for some React apps) then you can uncheck the dynamic / changing selectors (such as the 'HTML hierarchy') from the element match criteria.

ℹ️ Chameleon now flags this for you: if a selection still uses names your site generates automatically after you click, you get a plain-English warning right away (see "See how reliable your selection is" above), so you know which criteria to uncheck.

Use trial-and-error to uncheck selectors that might be the culprits (we suggest starting with 'HTML hierarchy' and 'class'). If removing these conditions causes the element to be matched again, you'll see a green border around that matching element on the page.

ℹ You can also add a data attributes to your elements when adjusting them in Chameleon, to help pinpoint the right one, while selecting one on your page.

🧑‍💻 Your developer can help here, or you can wait for the application to be updated (before publishing an Experience) and then make adjustments.

Did this answer your question?