VBA to click web button with dynamic elementID and repetitive class - html

I am quite new to VBA/html, trying to automate filling a form, but cannot even call it ... I searched the web for weeks tried many different code combinations, but none got me there.
The HTML code/element looks like this:
div title:"TextText" class:"text-truncate mb-2" id="abc_sidebar_startmenuitem" data-type="record" data-appid="82" data url="index.cfm?Xxx=&Yyy=">
i class="fa icon-app-82 mr-1 fa-fw">/i>
span class="clickable" id="ext-eng123">Text/span>
/div>
Problem is that class="clickable" is 30th appearance of clickable on the page, id="ext-eng123" is built from the text ext-eng and 3 variable unknown digits, always different.
Example of VBA code used:
Sub GetClick()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "https://company.application.com/home/index.cfm?Tab=home"
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
Dim objIE As Object
objIE = document.getElementByClassName("clickable")(29)
objIE.Click
End Sub
I tried over 10+ different code samples, including calling frame number, none worked, I am stuck.

Try the following code. It is hard to give any solution without playing with that site. They are always hypothetical.
Sub GetClick()
Dim IE As New InternetExplorer, Html As HTMLDocument
With IE
.Visible = True
.Navigate "https://company.application.com/home/index.cfm?Tab=home"
While .Busy = True Or .ReadyState < 4: DoEvents: Wend
Set Html = .Document
End With
''If the problem still persists, make sure to put here some delay
Html.querySelector(".clickable[id^='ext-eng']").Click
End Sub
Another approach might be something like (if the word "Text" didn't appear anywhere with the same class name):
For Each elem In Html.getElementsByClassName("clickable")
If InStr(elem.innerText, "Text") > 0 Then elem.Click: Exit For
Next elem
Reference to add:
Microsoft Internet Controls
Microsoft HTML Object Library

Related

Loop through dropdown browsermenu with VBA

I need to loop through a dropdown list of a specific website and download the latest file for each entry. I managed to open the website and click the "submit" button, but I can´t find a solution to loop through all the dropdown entries.
Dim reportnr As String
Dim internetadress As String
Dim btn As Object
Dim IE As SHDocVw.InternetExplorer
reportnr = 10
internetadress = adress & reportnr
Set IE = CreateObject('InternetExplorer.Application')
IE.Visible = True
IE.navigate internetadress
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
While IE.Busy
DoEvents
Wend
For Each btn In IE.document.getElementsByClassName("btn btn-primary")
If btn.name = "submit" Then
btn.Click
End If
Next btn
The code on the website regarding the dropdown below.
I tried several approchoaches, but I´ll get error or just nothing happens. Last thing I tried is this :
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
Dim ie As New InternetExplorer, numberOfOptions As Long, i As Long
With ie
.Visible = True
.Navigate2 "https://www.amazon.com/belif-True-Cream-Aqua-Korean/dp/B00H4GOAZO/ref=pd_cart_crc_cko_cp_2_2/139-8277217-3794320?_encoding=UTF8&pd_rd_i=B00H4GOAZO&pd_rd_r=e154e278-8a11-4ab0-8173-5d0dbaff1938&pd_rd_w=Hm8FW&pd_rd_wg=Hpv4X&pf_rd_p=eff166ab-25d2-4f2c-a51d-0a0e86061f9d&pf_rd_r=EVT26E6K7CV8T1QMTY7H&psc=1&refRID=EVT26E6K7CV8T1QMTY7H"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
numberOfOptions = .querySelectorAll("#quantity option").Length 'gather option tag element children of parent select element with id quantity
For i = 1 To numberOfOptions
.querySelector("#quantity [value='" & i & "']").Selected = True
ActiveSheet.Cells(i, 1) = i
Next
Stop
End With
End With
End Sub
Source: VBA loop through dropdown elements from web page and download to excel sheet
Thanks in advance for help.
From the website you provide, you can try the following code to loop through the dropdown list to choose the option you want:
Set lists = IE.document.getElementsByClassName("active-result") 'get all the options li elements
For Each liElement In lists
If liElement.innerText = "something" Then
liElement.setAttribute "class", "active-result result-selected"
End If
Next liElement
I find that the chosen option has different class which is active-result result-selected, so I think maybe you can change the class to select the option.

How can I get a HTML button reference in IE's DOM from VBA when it lacks an id=""?

I'm trying to use VBA macro to automatically log in on a website and I'm struggling to find a way how to click "Log on" button.
The button code:
<div class="GPSV45OBMKD GPSV45OBJLD" style="line-height: 20px;">Log on</div>
Already tried a lot of solutions posted by other users here, yet non of them worked.
Here's the code:
Sub test()
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://login.transporeon.com/login/"
With ie
.Visible = True
.Navigate my_url
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
ie.document.getElementById("emailForm_email-input").Value = "admin"
ie.document.getElementById("emailForm_password-input").Value = "54321haha"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End Sub
I am going to assume those classes are dynamic and use instead the relationship of type for child divs and the id of the parent
ie.document.querySelector("#emailForm_submit div div").click
If they are not dynamic you can use one of the compound classes for the element
ie.document.querySelector(".GPSV45OBJLD").click

Can't populate results from a website using vba

I've created a script in vba using IE to get the content poputaled upon selecting an option from dropdown. In the site's landing page there is a dropdown named Type. When I click on Type, I can see few options there and I would like to click on corporate bond among them. When the result are generated I would like to parse the WKN.
I've tried to populate results using my current script by clicking on the dropdown and selecting an option from there. However, it clicks on the dropdown and select the desired option but can't produce any result in InternetExplorer.
How can I populate and parse the result selecting an option from dropdown?
My attempt so far:
Sub SelectItem()
Const link = "https://www.boerse-stuttgart.de/en/tools/product-search/bonds/"
Dim IE As New InternetExplorer, Html As HTMLDocument, post As Object, T As Date
With IE
.Visible = True
.navigate link
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Application.Wait Now + TimeValue("00:00:15")
Html.querySelector("#bsg-filters-btn-bgs-filter-3 .bsg-btn__icon").Click
Application.Wait Now + TimeValue("00:00:05")
Html.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-scrollbox label[for='bsg-checkbox-3053']").Click
Application.Wait Now + TimeValue("00:00:05")
Html.querySelector(".bsg-dropdown__footer button[type='button']").Click
End Sub
You are familiar with timed loops so you can add timeouts to the following. I use css selectors through out to target elements. I monitor for the disappearance of the whirring circles as measures of page load at various points. At the end I monitor for presence of results items nodeList nodes.
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetCodes()
Dim ie As New InternetExplorer, btn As Object, items As Object, i As Long
With ie
.Visible = True
.Navigate2 "https://www.boerse-stuttgart.de/en/tools/product-search/bonds"
While .Busy Or .readyState < 4: DoEvents: Wend
Do
Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0
.document.querySelector("#bsg-filters-btn-bgs-filter-3").Click
.document.querySelector("#bsg-checkbox-3053").Click
Set btn = .document.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-btn__label")
Do
Loop While btn.innerText = "Close"
btn.Click
Do
Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0
Dim count As Long
Do
On Error Resume Next
Set items = .document.querySelectorAll(".bsg-table__tr td:first-child")
count = items.Length
On Error GoTo 0
Loop While count = 0
For i = 0 To items.Length - 1
Debug.Print items.item(i).innerText
Next
.Quit
End With
End Sub

Why my google search macro is returning error 424 - Object required?

I have this simple macro that opens up IE and type a word in search box. It was working before, but now is returning
Error 424 - Object required.
Any ideas on what is happening? Is there any lib reference that I'm missing?
Sub AcessaPaginaDados()
Dim xobj As HTMLDivElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate ("https://www.google.com/")
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set xobj = ie.document.getElementById("lst-ib")
Call xobj.setAttribute("value", "teste")
End Sub
That id doesn't appear against the input box when I open google. You can use an attribute = value selector to target the title attribute of the search box. Right-click inspect the search box and see what your local language attribute value is for title=. In U.K. the value is Search. Use whatever your local value is.
Element html:
Other:
Also, .navigate2 is the preferred method.
If the page is slow loading you may need a loop until element present.
Option Explicit
Public Sub AcessaPaginaDados()
Dim xobj As HTMLDivElement
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate2 "https://www.google.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
Set xobj = .document.querySelector("[title=Search]")
xobj.Value = "teste"
Stop
.Quit
End With
End Sub

vba click a website button

I have some code that used to work fine, but for some reason it has stopped selecting the tab that says "your leagues".
The code pulls live football scores into excel.
The only bit I need help with is where I need to navigate from all games to your leagues (the code does not recognize the button and so is not clicking it). Any help will be appreciated. Thanks.
'Define Variables
url = "http://www.futbol24.com/Live/livenow/"
'Open url
ie.Visible = False
ie.Navigate url
frmbusy.lbstatus.AddItem "Navigating to Web Page"
busywait
'Navigate from ALL Games to Your Leagues
Set tagname = ie.Document.getelementsbytagname("*")
For z = 0 To tagname.Length - 1
If tagname(z).ID = "f24com_i18n_btnChooseLeague_your" Then
tagname(z).Click
busywait
End If
Next
You can use a querySelector to target
#f24com_btnChooseLeague_choose > a > span
This is the span tag within an a tag with element with id #f24com_btnChooseLeague_choose
You can shorten this simply to #f24com_btnChooseLeague_choose > a
Option Explicit
Public Sub ClickChooseLeagues()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate "https://www.futbol24.com/Live/livenow/"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("#f24com_btnChooseLeague_choose > a").Click
Stop
End With
End Sub