-1

Here I am trying to replicate one issue I am facing in my secured network using a live example so that some one can help me to find a solution for this.

In fact I open one question (VBA: How to click a drop down in a web page with the help of HREF and CLASS HTML tag avilable ) for the same issue but I cant find a solution yet.

Expected result: The code which help me to open a web page (https://www.amazon.in/) and navigate to a dropdown (Today's Deal) from a menu bar using HTML tag CLASS and HREF only. [The sample (website) give here have "tabindex" as well but please dont use that as a solution because in my really scenario I dont have any other tags other than href & class (Please find the screen shot of HTML tags I have for the real webpage)]

Real scenario, just keep it here as a reason why i dont want to user tabindex as a solution Img1: Real scenario, just keep it here to make you understand that I only have these two tags

The VBA code

Sub Click_aLinkorDropdown()

Dim MyHTML_Element As IHTMLElement
Dim MyURL As String

'On Error GoTo Err_Clear
MyURL = "https://www.amazon.in/"
Set MyBrowser = New InternetExplorerMedium
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True

For Each MyHTML_Element In HTMLDoc.getElementsByClassName("nav-a")
If InStr(MyHTML_Element.href, "/gp/goldbox/ref=nav_cs_gb") Then 
MyHTML_Element.Click
Exit For
Next

'Err_Clear:
'If Err <> 0 Then
'Err.Clear
'Resume Next
'End If
End Sub

HTML TAG

href='/gp/goldbox/ref=nav_cs_gb' class='nav-a' tabindex='22'>Today's Deals</a><a 

Issue I am facing by using this code The web page open and when it reach to click the link I am getting two types of error 1: > Runtime error 438: Object does not support this property or method

2: > Runtime error 424: Object required

And i also tried one of the post available in StackOverflow but this is not working for me

mithun nair
  • 136
  • 11

1 Answers1

1

Use the following code and remember to enable the references Microsoft Internet Controls and Microsoft HTML Object Library:

#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub test()
Dim IE As Object
Dim HTMLDoc As Object
Dim sURL As String

' CREATING OBJECT
Set IE = CreateObject("internetexplorer.application")

sURL = "https://www.amazon.in/"
' WEBPAGE NAVIGATION
With IE
.Navigate (sURL)
.Visible = True
End With

WaitIE IE, 2000

' CLICK ON LOG IN
Set HTMLDoc = IE.Document
Set buttonclick = HTMLDoc.getElementsByClassName("nav-a")

For Each Element In buttonclick
    If Element.href = "https://www.amazon.in/gp/goldbox/ref=nav_cs_gb" Then Element.Click
Next Element
WaitIE IE, 5000

'IE.Quit
'Set IE = Nothing
End Sub

Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.READYSTATE = 4 Or Not IE.Busy
End Sub

You can refer to this answer for a similar answer code.

danieltakeshi
  • 887
  • 9
  • 37
  • Thanks @danieltakeshi this code is working for Amazon but not working for the one where I really want this, will u guide my when `Debug.Print` is not generating any values under Immediate window. I set value like this `Set buttonclick = HTMLDoc.getElementByClassName ("Logs")` & then `For Each Element in buttonClick _Debug.Print element.href _Next _End Sub` but when I run, the code just jump from `For Each Element in buttonClick` to `End sub` without providing any values & executing below codes, r u able to guide me what will be the reason or how can we resolve these kind of issues... – mithun nair Nov 23 '17 at 10:36
  • You should create another question with the website you want. Because each website has your own structure and you have to understand it in order to Get the Elements. – danieltakeshi Nov 24 '17 at 10:42
  • Thanks @danieltakeshi, the web site I am referring to is on a secured server therefor we cant access the same, is there any solution to get my issue fixed – mithun nair Nov 24 '17 at 12:49
  • Get the HTML on a .txt and post your question. Use [this code](https://pt.stackoverflow.com/a/254899/75104) to get the html on a .txt. Or study more on DOMs – danieltakeshi Nov 24 '17 at 12:55
  • 1
    Thanks @danieltakeshi, now it seems like my effort to automate the website ends here, Just see what I got after performing that code you shared `/*<![CDATA]"{*/html{display:non;} /*]]>*/ /*<![CDATA[*/ if(self==top) { document.documentElement.style.display = 'block'; } else {top.location = self.location;} /*]]>*/` because this a secured network i guess they have some layer of protection on this.... Thanks anyways for guiding me till here... – mithun nair Nov 26 '17 at 16:48