Clicking a button with variable name on an accordeon

Hello everyone, i have this problem:

the robot i’m making needs to click a button inside an accordion, the text of the button is variable (with an id dependant item)

the accordion and the button are like this:

div class= “active title accordion-title” <i aria-hidden=“true” class=“dropdown icon” </iContent area title </div<div class=“content active” style=“padding: 1rem; background-color: rgb(238, 238, 238);”<div <button class=“ui basic button” style=“padding: 0px; box-shadow: 0px 0px 0px 0px; margin: 0px; text-align: inherit;”{here’s the numerical id}{and here’s the complementary text}</button </div </div </div </div

i tried using ‘Get WebElements’ but the list lacks the text needed to be checked, and probably need a quicker way to search directly for the id but i’m clueless if Selenium has a functionality for that

You could think of the locator as a larger path, not just focusing on the button itself, but as series of elements getting to the button. This could involve starting even further up the DOM than the accordion. I would describe this as a composing a unique locator problem and look for a solution and techniques for solving those type of problems. In the generic sense I do that by asking a series of question to myself about my problem/solution. In this case I might try …

Is this the only button element under the accordion? If so then I don’t need to know much more then it is the button element.
What are the parent elements above the accordion? Do I need to make a locator that traverse more than just the element I want to select?

[… that is my rough start and thinking more or answering these questions would lead me further down the path towards a solution …]

1 Like

Hi Sylvaranth,

if the the complementary text is constant then it could be quite simple, just use the complementary text to identify the button i.e. if the complementary text is “Taxi” and you have buttons

  • 13 Taxis
  • 37 Taxis
  • 1 Taxi
  • 8 Taxis

and you want the second button regardless of the number you could use an xpath like (//button[contains(text(), "Taxi")])[2]

The inner part //button[contains(text(), "Taxi")] matches any button that contains the string Taxi (Taxi is a substring of Taxis), the outer part simply says I want the second match.

You could also do something like (//button[@class="ui basic button"])[2] as long as no other buttons in your page have the same class combination of “ui basic button”

Dave.

hi, no, in this case both the id part and the complementary part of the text are variable, i tried also with a direct search with Click Button to locate the text but it seems it doesn’t work for this case

Hi Sylvaranth,

The key to solving this type of problem is to find something that remains constant across page loads that you can use as a reference point, it could be an element with a non dynamic id, a piece of text that’s always in the same place on the page or something like that, you use an xpath to the reference element and then add the relative path from the reference element to the element you want.

Without access to the actual html/web page I can’t offer more than that.

Dave.

1 Like