VBA Web Automation on a dynamic site - html

I am trying to scrap barcode owners from this site : http://gepir.gs1.org/index.php/search-by-gtin
I am receiving "Object doesnt support this property or method" error msg because of the final line. I tried to extract it with class and tag but failed.
I am trying to extract the company name from the dynamic table.
Sub aa()
Dim ie As New SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Dim aaa As Object
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
ie.Visible = True
ie.navigate "gepir.gs1.org/index.php/search-by-gtin"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Debug.Print ie.LocationName, ie.LocationURL
Set doc = ie.document
ie.document.forms("searchbygtin").elements("keyValue").Value = "685387379712"
ie.document.forms("searchbygtin").elements("method").Click
Debug.Print ie.document.getElementsBytag("tr")(7).innerText
End Sub
Any help would be great,
Thanks

You need TagName
Debug.Print ie.document.getElementsByTagName("tr")(7).innerText
I get a captcha though so I doubt you can complete this task in this way. Selenium and IE both seem to trigger the captcha, at least for me.
If not for the captcha I would re-write your script as follows (perhaps with a loop to make sure tr element is present):
Option Explicit
Public Sub GetLastChangeDate()
Dim ie As New SHDocVw.InternetExplorer, doc As HTMLDocument
Const URL = "gepir.gs1.org/index.php/search-by-gtin"
With ie
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Set doc = .document
Debug.Print .LocationName, .LocationURL
doc.getElementById("keyValue").Value = "685387379712"
doc.getElementById("submit-button").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Debug.Print .document.getElementsByTagName("tr")(7).innerText
End With
End Sub

Related

How to copy/paste data from excel to a webpage (VBA)?

So I'm trying to copy/paste data from excel into a webpage text box using VBA. However, my problem is that if I have, for example 3 or 4 rows of data copied, when pasting the values into the webpage using vba, only 1 row will be copied rather than all the rows.
Here is my code:
.Document.getElementsByTagName("textarea")(0).Value = ActiveCell.Value
Any ideas? If I take out the (0) I get an error:
object doesn't support this property or method.
This is some code of mine that works:
Sub FillOutInvoice(Account As Account)
Dim ie As InternetExplorer
Dim elem As HTMLLinkElement
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate Settings.ErpAddress
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
With ie.document
.getElementsByClassName(Settings.InputClass)(0).innerText = Account.InvoiceNumber
.getElementsByClassName(Settings.InputClass)(1).innerText = Day(Account.InvoiceDate)
End With
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Application.Wait Now + #12:00:02 AM#
ie.Quit
End Sub
As you see, the needed property is .InnerText and not .Value.
Here is an example of sending a range of rows text to a textarea element using the clipboard
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub InsertData()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.google.com/search?q=google+translate&rlz=1C1GCEB_enGB815GB815&oq=google+tran&aqs=chrome.0.0j69i57j0l4.2057j0j7&sourceid=chrome&ie=UTF-8"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim clipboard As Object
Set clipboard = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
ActiveSheet.Range("A1:A3").Copy
With clipboard
.GetFromClipboard
ie.document.getElementsByTagName("textarea")(1).innerText = .GetText
End With
Application.CutCopyMode = False
Stop
.Quit
End With
End Sub
Contents of Range("A1:A3")

Activating a search button via .Click

I'm using IE11 with the HMTL Object Library and Internet Controls references activated.
There's no element ID on the button but am able to use ie.Document.getElementsByClassName by adding some html and xml declarations thanks to this post.
I'm taking a name and city, state from Excel and plugging it into the website then clicking the search button.
This is where my error occurs.
Run-time error '438': Object doesn't support this property or method.
HTML:
VBA:
Option Explicit
Sub HGScrape()
'Application.ScreenUpdating = False
'we define the essential variables
Dim ie As Object
Dim my_url As String
Dim SearchButton, NameBox, AddressBox
Dim ele As Object
Dim x As Integer
Dim y As Integer
Dim IsOdd As Integer
Dim html_doc As Object 'HTMLDocument
Dim xml_obj As Object 'MSXML2.DOMDocument
my_url = "https://www.healthgrades.com/"
'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = New InternetExplorer
ie.Visible = True 'False ''''''''''''''''''''''
ie.Navigate my_url '"13.33.74.92" '("https://www.healthgrades.com/")
While ie.ReadyState <> 4
DoEvents
Wend
Set NameBox = ie.Document.getElementById("search-term-selector-child")
NameBox.Value = ActiveSheet.Range("A2")
Set AddressBox = ie.Document.getElementById("search-location-selector-child")
AddressBox.Value = ActiveSheet.Range("B2")
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerHTML = xml_obj.responseText
Set SearchButton = ie.Document.getElementsByClassName("autosuggest_submiter") 'id of the button control (HTML Control)
SearchButton.Click
While ie.ReadyState <> 4
DoEvents
Wend
I condensed your code a bit. You really do not need to set every element to a variable. This just wastes resources in the long run.
Use the ClassName submiter__text to grab your submit button, and it's index of 0.
Sub HGScrape()
Const sURL As String = "https://www.healthgrades.com/"
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate sURL
While .Busy Or .ReadyState < 4: DoEvents: Wend
.Document.getElementById("search-term-selector-child"). _
Value = ActiveSheet.Range("A2")
.Document.getElementById("search-location-selector-child"). _
Value = ActiveSheet.Range("B2")
.Document.getElementsByClassName("submiter__text")(0).Click
While .Busy Or .ReadyState < 4: DoEvents: Wend
End With
End Sub
"..Why was the "submitter_text" class the correct one?"
The best way to explain it is to show you. If you are unsure what selection to make, then right-click the element and choose "Inspect Element" and look around the highlighted line.

VBA Auto Login Web page and fetch the data

I am trying to login Web page and fetch data however my login details are not getting update, i have tried all possibilities code from your forum, nothing is working for me
Below is my code, am getting attached error
Sub test()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://portal.expeditors.com/expo/login"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "username" Then
objCollection(i).Value = "bom-sumand"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
'Clean up
Set ie = Nothing
End Sub
I would use the available ids rather than looping to find the input boxes and sign in. These are much faster selector methods. You can add a .Focus. Also, swop InternetExplorer for InternetExplorerMeduim in some cases.
If problem continues check your internet settings in case site is blocked.
Open the URL via creating an IE instance direct.
Option Explicit
Public Sub Login()
Dim ie As New InternetExplorer 'InternetExplorerMedium
Const MAX_WAIT_SEC As Long = 5
Dim t As Date, ele As Object
With ie
.Visible = True
.navigate "https://portal.expeditors.com/expo/login"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
DoEvents
On Error Resume Next
Set ele = .getElementById("j_username")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
With ele
.Focus
.Value = "bob"
End With
With .getElementById("j_password")
.Focus
.Value = "penny"
End With
.getElementById("signInBtn").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
Macro trying and failing to open a second instance of IE try this.
Sub test()
Dim ie As Object
Dim redURL As String
Dim objCollection As Object
Dim i As Integer
redURL = "https://portal.expeditors.com/expo/login"
On Error Resume Next
Set ie = GetObject(, "InternetExplorer.Application")
If Err Then
Set ie = CreateObject("InternetExplorer.Application")
End If
On Error GoTo 0
ie.Visible = True
ie.Navigate redURL
Do While ie.Busy
Loop
End Sub

Vba getElementById cause Run-time error '424'

Really appreciate if anyone can help.
The part of code always run into Run - time Error:
The HTML part is in the picture:
Thanks for attention.
Sub HTML_Table_To_Excel()
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.application")
With objIE
.Visible = True
.navigate ("http://www.global-rates.com/interest-rates/libor/libor.aspx")
End With
While objIE.Busy
Wend
Dim HTMLDoc As Object
Set HTMLDoc = objIE.document
Dim EuroButton As Object
Set EuroButton = HTMLDoc.getElementById("btn_eur")
objIE.Quit
Set objIE = Nothing
......
End Sub
It's not an element ID - it's a tag name; Set EuroButton = HTMLDoc.getElementsByTagName("btn_eur") then when you need to order it: EuroButton(0).[command here]
You were just fine with your selector. It does have that id. The problem is you need a proper wait for page load before attempting to click. Try the below. I have also updated to .navigate2 and removed the unnecessary ().
Option Explicit
Public Sub HtmlTableToExcel()
Dim objIE As Object, HTMLDoc As Object, euroButton As Object
Set objIE = CreateObject("InternetExplorer.application")
With objIE
.Visible = True
.Navigate2 "http://www.global-rates.com/interest-rates/libor/libor.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
Set HTMLDoc = .document
Set euroButton = HTMLDoc.getElementById("btn_eur")
euroButton.Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<==delete me later
'-----
'.Quit
End With
End Sub

Excel VBA Select Option Website Dropdown List and Click

I am trying to copy the response from the website, but I have to select one of the options from the dropdown list so the page can refresh.
So far, I've managed to open IE and click the dropdown button, but I cannot select the option, don't know why. Please find the a sample of the code below.
Sub OpenWebSelectClickSaveHtmlFile()
Dim URL As String
URL = "http://formulaacademia.com.br/academia"
Set myIE = CreateObject("InternetExplorer.Application")
With myIE
.navigate URL
.Visible = True
Do While .Busy = True Or .readyState <> 4
Loop
DoEvents
Set selectitems = myIE.document.getElementsByTagName("button")
For Each i In selectitems
i.Click
Next i
End With
End Sub
Rgds
It should be something like this. Change the relevant parts to suit your needs.
Sub passValueToComboBox1()
Dim ie As Object
Dim oHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://peterschleif.bplaced.net/excel/combobox/index.php"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)
If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "com.db.moap.report.FUBU7"
For Each oHTML_Element In ie.document.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
End Sub