Web Automation with Selenium

Robotiq supports the Selenium framework for web automation. One of the most challenging aspects is selecting the right Selenium selector. Whether you are a beginner or an advanced user, this guide will help you.

Sometimes, finding the correct selector is straightforward. Other times, certain web pages make it more difficult. In Robotiq, you can choose from several Selenium selector types: ID, name, class name, CSS selector, XPath, and more.

For any selector you use, it is crucial to ensure that it is unique and does not change between sessions. To find selector, press F12 to open the Developer Tools, or right-click an element and select Inspect.

 

XPath Overview

 

XPath is a powerful tool for writing expressions that locate HTML elements. It can search for elements such as tables, images, and text with specific values. Below are some examples:

//td[text()=’blablaa’]

  • Find a td element in the HTML three where text in the tag is blabla

//img[@src=’https://link_to_some_image’]

  • Find an img tag in the HTML three where the src attribute of the tag is https…

//table/tr/td[2]

  • Find a table tag in the HTML tree, it’s first child tr element and second td child of tr
  • Index is not zero based

//div[@class='my-class']/parent::*

  • Find a div element with class my-class and go to the parent node (any type of node)

//table/following-sibling::a

  • Find a table element and then find the following sibling a element

//a/preceding-sibling::*[1]

  • Find a element and then it’s first preceding element


//div[text()[contains(.,'ABC')]] 

  • Find div element where text contains ABC string

//input[@id='name' or @name='name']

  • Find input element where id is ‘name’ or name attribute is ‘name’

NOTE: For more, see the XPath/CSS Cheat Sheet by ScrapingBee.

Understanding XPath Syntax

 

// → Search anywhere in the HTML document tree (not just from the root).

/ → Navigate to a direct child element.

@ → Refers to an attribute (e.g., @id, @class, @src).

[] → Adds a condition or filter (e.g., “the second element” or “element with specific text”).

text() → Refers to the text content inside an element.

Common HTML Elements

 

HTML is built from tags (elements), each with a specific purpose:

<td> → table data cell (a single cell inside a table row).

<tr> → table row.

<table> → table container (holds rows and cells).

<img> → image element (src specifies the image URL).

<div> → generic block container (used for layout).

<a> → anchor element (link).

<input> → input field (text box, checkbox, radio button, etc.).

<label> → label element (descriptive text for an input).

These are called HTML tags or elements.

XPath Structure

 

To search through an HTML document:

  • Start with // to search anywhere.
  • Add the HTML tag you want to target.
  • Optionally, add a condition in brackets [].
  • Use / to go deeper into child elements.

example: //table/tr/td[2]
Steps:

  1. // → Search anywhere.
  2. table → Find a <table> element.
  3. tr → Go one level deeper into its <tr> (row).
  4. td[2] → Select the second <td> (cell) in that row.

Quick Tip: Copying XPath

 

In most browsers, you can quickly generate an XPath by:

  • Right-clicking the element.
  • Selecting Inspect.
  • In the Developer Tools, right-clicking the highlighted HTML code.
  • Choosing Copy → Copy XPath.

However, keep in mind that auto-generated XPaths are often absolute (e.g., /html/body/div[3]/...), which can break easily if the page structure changes. It is usually better to edit them into a relative XPath that relies on stable attributes such as id, name, or meaningful text.

Validating XPaths

 

You can test your XPath in the Developer Tools (Ctrl+F) to see if it matches only one element. If the search result is 1 of 1, you’ve found a unique selector.

 

 

Dynamic Selectors

 

If multiple fields share similar XPaths that differ only in one value, you can create a dynamic selector with a placeholder variable. This allows you to replace the variable at runtime.

This is useful when looping through table rows, where each row needs a slightly different selector. By using a dynamic selector in a loop, you always target the correct element.

For example, when handling a changing field like ticketID, you can insert the current ticket ID into the XPath at runtime. This makes it possible to dynamically search and interact with different elements during execution.

Handling Complex Cases

 

Of course, there are situations where you can't find a selector, so it will be helpful to use the Advanced Click Step or the Click and Type Step with the value of "Wait for window activation" set to False. If you need assistance with selecting the correct image and anchors, click on this link.

Advanced Selenium Access

 

If you need Selenium functionality not yet supported by Robotiq, you can use the Script Step to directly access the Selenium driver reference.

IWebDriver driver = (IWebDriver)VR["_selenium_driver_script_reference_chromeReference"];

Here, chromeReference is the name of your driver reference you used in step.


Note: Code in the Script Step is custom and must be maintained by you. Robotiq.ai does not provide support for it.
 

Practice Resources

 

If you would like to practice building selectors and automating web interactions, you can use demo websites designed specifically for testing automation:

These sites provide sample forms, tables, and dynamic elements that allow you to safely try out XPath and other Selenium selectors without affecting real applications.

Was this article helpful?