Skip to main content

Robot Task Activities - Element Path​

The element path settings tell a Web Macro activity which HTML element on the page to act on. Many Web Macro activities have a Source tab for this, with three ways to specify the element:

  • Relative
  • By position
  • By search

Relative​

Element path specifies the path to the element. Both a CSS selector and an XPath expression are supported. A CSS selector on its own is enough, but the full format of the path string is:

pathCss[%]pathHtml[%]pathXPath
  • pathCss: a CSS selector
  • pathHtml: an additional internal format for the path to the element
  • pathXPath: an XPath expression
  • [%]: the separator between the parts

How the path is resolved​

A recorded action may store the path in the full format. When you configure an activity yourself, the path does not need all three parts.

At runtime the path is split on the [%] separator and the element is searched for in this order:

  1. By the third part (pathXPath), if there is one.
  2. If it is not found by XPath, by the second part (pathHtml).
  3. If it is still not found, by the CSS selector (pathCss).

If there is no [%] separator, the whole value is treated as a CSS selector.

To find an element by XPath only, put the expression after two separators:

[%][%]/HTML/BODY/APP-ROOT[1]/DIV[2]/APP-RPA1[1]/DIV[1]/DIV[2]/FORM[1]/INPUT[1]

CSS selector examples​

Given this HTML:

<html>
<head>
<title>Title</title>
</head>
<body>
<span id="rX8Qvb-cnt" class="toolbarbutton-content">Hello, World!</span>
</body>
</html>

Any of these selectors finds the span element:

  • Class selector: .toolbarbutton-content
  • ID selector: #rX8Qvb-cnt
  • Type selector: span
  • Attribute selector: span[id="rX8Qvb-cnt"]
  • Child selector: html > body > span

XPath examples​

[%][%]//*[@id="rX8Qvb-cnt"]
[%][%]/HTML/BODY/SPAN[1]
[%][%]//span[contains(., "Hello")]

Frame path​

Frame path specifies the path to the HTML frame element that contains the element, as a CSS selector.

In most cases this field can be left empty. Fill it in only when the element is inside a frame, that is when the page contains a <frame> or <iframe> element. For example, a Frame path of FRAME:nth-child(2) with an Element path of #ddlCustomer.

Further reading:


By position​

Locates the element by its X and Y coordinates on the page. A position stops matching as soon as the page layout changes, so prefer Relative or By search.


Searches for elements with a CSS selector, which is passed to the JavaScript querySelectorAll function. Use it when an element's path changes between runs.

  • Selector: the CSS selector passed to querySelectorAll, which defines the list of candidate elements
  • Attribute: the element attribute whose value is tested; the element properties innerHTML, outerHTML, innerText and outerText can also be used
  • Regex value: the regular expression the attribute value is tested against, which filters the list of candidates
  • Position: which of the filtered elements to use

The search runs in three steps:

  1. Build a list of the HTML elements that match the CSS selector.
  2. Filter the list by testing the attribute or property value against the regular expression.
  3. Take the element at the position given in the settings.

For example, a Selector of span, an Attribute of innerText, a Regex value of Hello and the first position returns the first span on the page whose visible text contains Hello.

Further reading:


Additional notes​

When you configure an activity yourself, the fastest way to find the right CSS selector or XPath expression is the web browser's inspector.

Some selectors include quotes. In paths, use double quotes, not single quotes.

For a walkthrough of using the inspector to build a selector for a form field, see Web Input - Element Path.


Tips​

  • Prefer selectors built on id and name attributes. They are the most stable and the least likely to change when a page is updated
  • Avoid purely positional XPath such as /HTML/BODY/DIV[3]/INPUT[1]. It breaks as soon as the page structure changes
  • Test a selector in the browser console with document.querySelector('your-selector') before you use it in VisualCron, to confirm it returns the element you expect
  • Use By search when several similar elements exist and the one you want is set apart by its text or an attribute value rather than by its position in the document
  • If the element may be missing, check Allow to ignore the element on the Source tab, so that the sequence is not stopped when the element is not found. See Error Handling