Question

Best way to iterate over a webpage buttons that are dinamicaly generated

  • 15 March 2024
  • 7 replies
  • 247 views

Badge +1

Hi all, i’m trying to automate extraction of information from a website. The page that lists the content has a button for each line, that needs to be clicked to get more information. This buttons are generated at run time acording to the content of the page and I honestly can find any difference in their html definition. What would be the best technique to interate the buttons on this page ? Each page contains 10 of these buttons and then “next” must be clicked.

I’m not aware if it would be possible to repeat all the capture actions that are done to get the information, like a function that could be called once we reach a certain html page. If this could be done, I could record the clicking on each one of those 10 buttons and call the function that extracts information. Otherwise I would have to repeat everything …

thank you,

Pedro


7 replies

Badge

Hey there,

There's a straightforward way to tackle this.

You'll need a while loop to continually check if the "next" button is available. Within this loop, continuously verify if the "next" button is clickable or available, which can be determined through capture.

Within this loop, you'll need another loop. Since each page contains multiple items that need iteration, you can create a loop that dynamically adjusts based on the number of items present. This way, your counter adapts to the number of elements on each page, ensuring comprehensive coverage.

Within this inner loop, select the correct element using the capture method. There are various ways to achieve this, such as using XPath to select the right button on the website. Ensure that the counter dynamically adjusts to select the appropriate button, ready to be clicked.

With this setup, you'll have your business logic in place, adaptable to variations in the number of elements on each page.

 

Badge +1

Thank you. I understand the logic of course, my only problem is how to programaticaly select the different buttons on the row because all seem to have the same name. I will try to see if their XPATH is different, maybe thats the option.

Userlevel 4
Badge +14

@pcasqueiro, Can you please share the xPath of multiple buttons, so that we can identify which section is changes and then pass a counter variable to make it dynamic.

Badge +1

Hi Zaid,

thank you for your help. I really don’t understand how can I place a capture inside a loop, changing the path dinamically.

A little context so that you can point me in the right direction. Each row (with a maximum of 10 por page) has a small Action Menu implemented with a DIV, that we need to click in order to access the Ver Detalhes (see details) option.

On the button of this page, there is a link for the next page “Seguinte”. On the last page, this link is greyed out.

So I guess the outer loop must be until we find this string Grayed Out and its now a LABEL, not an anchor anymore.

 

After clicking the small action menu, we have to choose the correct action we want, in this case Ver Detalhes (see details):

 

I have capture the properties of the menu (button) and option (Anchor) of the first 4 rows:

ROW 1

Click Small Menu
HTML Tag: BUTTON
DOMXPath: //button[@id='dropdownMenuButton']
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|1|-1|1|1
HTML Tag: A

Click inside Small Menu in Ver Detalhes
DOMXPath: //div[@id='single-spa-application:@agt/ip']/app-root-ip[1]/div[2]/app-lista-consulta[1]/div[1]/div[1]/div[1]/app-lista-predios[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/span[5]/div[1]/div[1]/a[3]
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|1|-1|1|-1|-1


ROW 2

Click Small Menu
HTML Tag: BUTTON
DOMXPath: //button[@id='dropdownMenuButton']
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|2|-1|1|1
HTML Tag: A

Click inside Small Menu in Ver Detalhes
DOMXPath: //div[@id='single-spa-application:@agt/ip']/app-root-ip[1]/div[2]/app-lista-consulta[1]/div[1]/div[1]/div[1]/app-lista-predios[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[2]/span[5]/div[1]/div[1]/a[2]
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|2|-1|1|-1|-1

ROW 3

Click Small Menu
HTML Tag: BUTTON
DOMXPath: //button[@id='dropdownMenuButton']
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|3|-1|1|1
HTML Tag: A

Click inside Small Menu in Ver Detalhes
DOMXPath: //div[@id='single-spa-application:@agt/ip']/app-root-ip[1]/div[2]/app-lista-consulta[1]/div[1]/div[1]/div[1]/app-lista-predios[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[3]/span[5]/div[1]/div[1]/a[3]
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|3|-1|1|-1|-1


ROW 4

Click Small Menu
HTML Tag: BUTTON
DOMXPath: //button[@id='dropdownMenuButton']
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|4|-1|1|1
HTML Tag: A

Click inside Small Menu in Ver Detalhes
DOMXPath: //div[@id='single-spa-application:@agt/ip']/app-root-ip[1]/div[2]/app-lista-consulta[1]/div[1]/div[1]/div[1]/app-lista-predios[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[4]/span[5]/div[1]/div[1]/a[3]
Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|4|-1|1|-1|-1

Comparing:

BUTTON (Acções)
ROW 1: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|1|-1|1|1
ROW 2: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|2|-1|1|1
ROW 3: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|3|-1|1|1
ROW 4: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|4|-1|1|1

ANCHOR (Ver Detalhes)
ROW 1: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|1|-1|1|-1|-1
ROW 2: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|2|-1|1|-1|-1
ROW 3: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|3|-1|1|-1|-1
ROW 4: Path: 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|4|-1|1|-1|-1

 

So, in both cases, as expected, the Path is changing with an incremental number.

I guess the algorith would be someting like:

Do While

FOR n=1 to 10

BtnPath = “2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|$n$|-1|1|1”

Click on BtnPath

AnchorPath = “ 2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|$n$|-1|1|-1|-1”

Clink on AnchorPath

2 secondw Delay to allow for page to open

Do capture data

Click Back

NEXT n

If there is a LABEL called Seguinte Exit Do While

another alternative

Click Seguinte (if error, exit Do While)

Loop Do While

 

My main doubt is how to use a single Recorder: Capture to click both the Button and the Anchor …

 

thanks a lot,

Pedro

Badge +8

go to while loop then check if object exist, here you can select next button...this may be your outer loop and then in inner loop you can check for further automation . If you are using xpath do try using counter in place where you see fields changing and increment it.

Badge +1

Just to confirm, if I just change the PATH propertie (I assume using a var inside the string) the action will be done on the page object with that path ? all other properties will be ignored ?

Badge +1

So, I placed inside a Loop, a Recorder: Capture where I changed the PATH propertie to:

2|1|1|2|-1|1|1|1|-1|1|-1|1|1|-1|$strCounter$|-1|1|1

being strCounter the string of the loop Counter

Its always the same (first) menu that is being clicked

Any ideas ?

 

Reply