Excel Selenium VBA - Help with loop when class appears more than once

Js Smith

Board Regular
Joined
Jul 24, 2020
Messages
51
Office Version
  1. 2010
Platform
  1. Windows
Hi!
I am attempting to copy some tables from a website. The site is secure so I can't share.
I got the table part fine. My problem is there can be 1 or a variable number of tables and to get to them one must click it's corresponding "Show History" .
We could have one Button or ten, who knows?


VBA Code:
Dim iVal As Integer
iVal = bot.FindElementsByXPath("//*[@class='btn btn-primary btn-small margin10-r']").Count

For K = 1 To iVal

szoom = "0.50"                      ' to get everything visible
bot.ExecuteScript ("document.body.style.zoom = '" & szoom & "'")

        bot.FindElementsByXPath("//*[@class='btn btn-primary btn-small margin10-r']")(K).Click           '  <----   k works on the first loop but doesn't work when K=2

'                bot.FindElementsByXPath("//*[@class='btn btn-primary btn-small margin10-r']")(2).Click       <---- this works so I know 2 is correct

I can see in that K is being assigned 2 in the second loop as it's running, but get Runtime Error Index was outside the bounds of the array. If I debug and use 2 in place of the K as illustrated, it works fine.
This is in Chrome, Using Office 365 for windows.
What am I missing?

Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this version:
VBA Code:
Dim iObj as Object, I as Integer
Set iObj=bot.FindElementsByXPath("//*[@class='btn btn-primary btn-small margin10-r']")
debug.print "iObj count=" & iObj.Count

'see Note*
For I=1 to iObj.Count
    szoom = "0.50"                      ' to get everything visible
    bot.ExecuteScript ("document.body.style.zoom = '" & szoom & "'")
    iObj(I).Click
[CODE=vba]'...
'...

Note*: I don't know if the lines szoom and bot.ExecuteScrip(etc etc are necessary or useful in that position
The line debug.print will write to the "Immediate window" of the vba editor how many objects have been identified with that xpath

I just rewrote your code, cannot comment on how adequate it is to get the correct data
 
Upvote 0
Solution

Forum statistics

Threads
1,223,882
Messages
6,175,164
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top