RPA / Robocorp

Browsing / Navigation: using locators

Click on elements using text based locators

tasks.py
Copied!

from robocorp.tasks import task
from robocorp import browser

@task
def browser_locator_text_click():
    # Initializes a new page in the browser
    page = browser.page()

    # Clicks directly on an element containing the text 'Click here'.
    # This is the simplest way to click on an element based on its text.
    page.click("text='Click here'")

    # Clicks on a link (a) containing the text 'Click here'.
    # Useful when you know the text is specifically in a link.
    page.click("a:text('Click here')")

    # Clicks on a button containing the text 'Click here'.
    # Specific for buttons, ensuring the text belongs to a button element.
    page.click("button:text('Click here')")

    # Uses the locator method to find the first element with the text 'Click here' and clicks on it.
    # Useful when there are multiple elements with the same text and you need to specify which one.
    page.locator("text='Click here'").nth(0).click()

    # Finds the first link (a) with the text 'Click here' and clicks on it.
    # Combines element specificity with index selection.
    page.locator("a:text('Click here')").nth(0).click()

    # Finds the first button with the text 'Click here' and clicks on it.
    # Similar to the above but specific for buttons.
    page.locator("button:text('Click here')").nth(0).click()

    # Explicitly wait for an element before clicking.
    # Useful on pages that load content dynamically.
    page.locator("text='Dynamic Content'").wait_for()
    page.click("text='Dynamic Content'")

Click on elements using CSS selector-based locators

tasks.py
Copied!

from robocorp.tasks import task
from robocorp import browser

@task
def browser_locator_css_click():
  # Initializes a new page in the browser
  page = browser.page()

  # Clicks on an element with a specific ID.
  # IDs are unique per page, making this a very precise selector.
  page.click("#uniqueElementId")

  # Clicks on an element with a specific class.
  # Classes are common for styling; multiple elements can share the same class.
  page.click(".buttonClass")

  # Clicks on the first element that matches a complex CSS selector.
  # This example targets the first link (a) element within an element with a specific class.
  page.click(".containerClass a:first-of-type")

  # Clicks on an element based on its attribute value.
  # This example finds an input with a name attribute equal to 'email'.
  page.click("input[name='email']")

  # Combining CSS selectors for more complex scenarios.
  # This targets an element with a specific class within a list item (li) that is the first child of its parent.
  page.click("ul > li:first-child .specific-class")

  # Using CSS pseudo-classes.
  # This example clicks on an element that is the last child of its type within its parent.
  page.click("div:last-of-type")

Click on elements using XPath based locators

tasks.py
Copied!

from robocorp.tasks import task
from robocorp import browser

@task
def browser_locator_xpath_click():
  # Initializes a new page in the browser
  page = browser.page()

  # Clicks on an element using its XPath.
  # This example clicks on a button whose text is 'Submit'.
  page.click('xpath=//button[text()="Submit"]')

  # Clicks on an element based on its position in the document structure.
  # This example clicks on the first link (a) inside a specific div.
  page.click('xpath=(//div[@class="example-class"]/a)[1]')

  # Clicks on an element using a combination of attributes.
  # This example clicks on an input of type 'checkbox' that also has a specific id.
  page.click('xpath=//input[@type="checkbox" and @id="uniqueId"]')

  # Explicitly waits for an element to be visible before clicking.
  # Useful on pages that load content dynamically.
  locator = page.locator('xpath=//div[@id="dynamicContent"]')
  locator.wait_for()
  locator.click()