Using Index Variable in Locator to Find A Series Elements

1. Introduction #

Clicknium’s Locator supports a parametric locator, which can be utilized by users in their automation projects to replace variables or data. By doing so, the locator can match a series of elements rather than just a single one.

To illustrate how to use an index variable in the locator to identify a series of elements, there are two automation cases available.

Note: For more about the installation and the tutorial of Clicknium Automation, please refer to here.

2. Automation Case-1 #

Case Description : Scraping contact list on the slack client,Scraping contact list on the Slack client to obtain user information.

Slack channel – Contact list

After recording one contact item’s locator, the user modifies the locator with an index variable.

Then use loop traversal to locate each contact element and get information.

for i in range(1,13):
    dict = {"index":i}
    if not cc.is_existing(locator.slack.listitem_member, dict):
        continue
    elem_member = ui(locator.slack.listitem_member, dict)
    name = elem_member.get_text()

3. Automation Case-2 #

Case description:Scraping playlist from Spotify. It is an actual user scenario, which needs to get the playlist information from spotify.

First, locate “author” and “title” from one item in the playlist.

As the Spotify page is complicated and dynamic, users use “XPath” in the locator.
Modify the locator with the index variable.

Then use loop traversal to locate each item in the playlist and get information.

tab = cc.chrome.open("https://open.spotify.com/playlist/6iwz7yurUKaILuykiyeztu")
index = 2

titles = []
artists = []
links = []
while True:
    if not tab.is_existing(locator.chrome.open.div_title, {'index':index}):
        break
    title = tab.find_element(locator.chrome.open.div_title, {'index':index}).get_text()
    artist = []
    link = []
    element = tab.find_element(locator.chrome.open.div_author, {'index':index})
    authers = element.children
    for item in element.children:
        artist.append(item.get_text())
        link.append(item.get_property('href'))

    titles.append(title)
    artists.append(artist)
    links.append(link)
    index += 1
tab.close()
What are your feelings
Updated on 4 December 2023