1
votes

I'm trying to create a very simple vbscript but can't make it to do what I want. I have a webpage lets call it "www.testing.com" that has many buttons (over 10 of them) that look and code the same:

<button type="button" class="text-uppercase promotion__btn btn btn-primary btn-sm">Get started now</button>

As you can see there is no name or ID for the button so I didn't find any solution in all my search for how to click the first button among them all.

My code for opening the webpage is very simple:

set IE = createobject("internetexplorer.Application")
IE.statusbar = false
IE.menubar = false
IE.toolbar = flase
IE.visible = true

IE.navigate("www.testing.com")

wscript.sleep(2000)

I tried to use this code with no success:

For Each btn In IE.Document.getElementsByTagName("button")
      If btn.type = "button" Then 
             btn.Click()
             Exit For
      End If
Next

Appreciate the help. Thank you for your time


Edit:


As user Lankymart recommanded 'IE.document.getElementsByTagName("button")(0).Click() does click the first button on the page but not the button that I need it to.

The button that get clicked by this command has the code: <button class="strong-action-button icon-plus js-create-new-catalog full-width">Create New Catalog</button>

While I'm trying to click a button with the code: <button type="button" class="text-uppercase promotion__btn btn btn-primary btn-sm">Get started now</button>

As you can see the button that I want to click on has type="button" so I'm trying to find a way to match Lankymart command with a something else that will help me click button that has that type in its code.

I tried the folloing code but nothing getting clicked and I don't get any error message so I assume I'm doing something wrong:

For Each btn In IE.document.getElementsByTagName("button") 
     If btn.type = "button" Then 
          btn.Click()
          Exit For
     End If
Next

EDIT 2:


My code looks like that right now:

set IE = createobject("internetexplorer.Application")
IE.statusbar = false
IE.menubar = false
IE.toolbar = false
IE.visible = true
IE.navigate("www.testing.com")
wscript.sleep(8000)
For Each btn In IE.document.getElementsByTagName("button") 
     If btn.innerText = "Get started now" Then 
           btn.Click()
           End If
Next

Running that script open the website but nothing get clicked. I think the issue is that it only find one button tag which is "Create New Catalog". I tested it by switching the FOR loop to this code:

Set results = ie.document.all.tags("button")
For Each button In results
  WScript.Echo button.innerText
Next

After running this code the only output is "Create New Catalog" which I don't understand why is that. Searching within the "inspect element" of the code show that there is 51 tags in the page, so why would it only find that one and not the other 50 buttons?


Edit 3:


So I think the issue is the fact that document.getElementsByTagName return code from the HTML of the site I'm using but when I click "inspect" to see the code of the button I would like to click the code is whole different than the HTML code (The one that show if I click "view page source"). Can that be the issue? if so how do I fix it? If someone has an idea how I can achieve my goal in a different programming language - I'm open to suggestions, it doesn't have to be VBscript. Thank you

1

1 Answers

0
votes

The Solution for the main question is that:

This code search for buttons with tag that the text on the button is "Get started now". That way you basically do not need button ID or Name.

For Each btn In IE.document.getElementsByTagName("button") 
     If btn.innerText = "Get started now" Then 
           btn.Click()
           End If
Next

Then I had a different problem which even with that code it didn't worked. So the user SearchAndResQ pointed out that in my case the button was under iframe and in order to access it I need to navigate to the iframe and then look for the button that I want to click.

Thank you SearchAndResQ, JNevill and Lankymart for taking your time to help me.