Unable to click on webpage based on attribute results - excel

I am trying to navigate in "https://www.ford.co.uk/shop/price-and-locate/build-and-price-gf3#/catalogID/WAEGB-CE1-2015-B479FiestaCarGBR201950/?code=". I have to select Titanium in the model list. But i am not able to select it. Everytime the list of models will vary based on link.
Below is the code which i have tried.
Sub testcode()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "https://www.ford.co.uk/shop/price-and-locate/build-and-price-gf3#/catalogID/WAEGB-CE1-2015-B479FiestaCarGBR201950/?code="
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:03"))
Set htmldoc = ie.document
Set HTMLAs = htmldoc.getElementsByClassName("features-list-item feature-box")
For Each HTMLA In HTMLAs
Debug.Print HTMLA.getAttribute("id"), HTMLA.getAttribute("tabindex")
If HTMLA.getAttribute("id") = "feature-1" And HTMLA.getAttribute("tabindex") = 0 Then
HTMLA.Click
End If
Next HTMLA
Set HTMLAs = Nothing
Set HTMLA = Nothing
End Sub
The result should be webpage loaded and selected the titanium series. Kindly assist me achieve this.

HTMLA itself doesn't seem to have a click handler, but you can click on one of its child elements.
Try this:
If HTMLA.getAttribute("id") = "feature-1" And HTMLA.getAttribute("tabindex") = 0 Then
HTMLA.getElementsByTagName("img")(0).Click
Exit For
End If

Related

Trying to click a link in IE using VBA

Trying to work on a Macro to login into a website and then download some data. Currently I am having trouble getting it to click on a link in the site.
Code:
Sub GetHTLMDocuments()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "xxx.yyy"
Do While IE.readyState <> READYSTATE_COMPLETE
Application.Wait Now + TimeValue("00:00:03")
DoEvents
Loop
Application.Wait Now + TimeValue("00:00:02")
ShowWindow IE.hwnd, SW_MAXIMIZE
Set HTMLDoc = IE.document
Application.Wait Now + TimeValue("00:00:02")
Set ElementCol = IE.document.getElementById("periods")
For Each btnInput In ElementCol
If btnInput.Value = "selected" Then
btnInput.Click
Exit For
End If
Next btnInput
IE.Quit
End Sub
Here's the html code from the site:
I would like to change the class which state the selected (or unselected) of the "option" element, any idea?
also tried to print all the Elements by class name......
Debug.Print InStr(1, HTMLDoc.body.outerHTML, "periods", vbTextCompare)
Dim dd As Variant
dd = IE.document.getElementsByClassName("periods")(0).innerText
IE.document.querySelector("[title='periods']").Click
For Each link In IE.document.getElementsByTagName("*")
With link
If .innerText = "periods" Then
link.Click
End If
End With
Next

How To Click On Web Page Image Link?

I tried several different methods to select a radio button or click on the image. There is an onclick task that is to be triggered. I can't automate any further until this opens up.
Sub AirCan()
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim doc As MSHTML.HTMLDocument
Dim img As MSHTML.HTMLImg
On Error GoTo Err_Clear
MyURL = "https://XXXXXXXXXX.com/en/XXXXXXXXXXXXX"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.paymentAmount.Value = "111" 'Enter your email id here
HTMLDoc.all.groupPayment.click
Do While MyBrowser.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set allInputs = HTMLDoc.getElementsByTagName("img")
For Each element In allInputs
If element.getAttribute("src") = "/HPPDP/hpp/images/ca_visa.png" Then
element.click
Exit For
End If
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
CODE from the website, clicking the radio button or any of the 3 images will do what I need,

Excel VBA - Get all href links from a website

Hi there, hoping that someone can help me.
On this example link: https://www.academiadasapostas.com/stats/competition/brasil/26
I want to get all the href links which are the target of the "VS".
I'm trying examples like this one:
Sub ScrapeScores()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim TableSection As MSHTML.IHTMLElement
Dim TableRow As MSHTML.IHTMLElement
Dim TableCell As MSHTML.IHTMLElement
Dim RowText As String
IE.Visible = True
IE.navigate "https://www.academiadasapostas.com/stats/competition/brasil/26"
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy
Loop
Set HTMLDoc = IE.document
Set HTMLDiv = HTMLDoc.getElementById("competition-round-group-0")
Set HTMLTables = HTMLDiv.getElementsByTagName("a")
For Each HTMLTable In HTMLTables
Debug.Print HTMLTable.ID, "&", HTMLTable.className
For Each TableSection In HTMLTable.Children
Debug.Print , TableSection.tagName
Next TableSection
Next HTMLTable
End Sub
But with no success. I think I could use CSS with a SelectorAll, right? Since IE is going to get extinted, would be nice to use the CSS instead.
Thank you in advance for any answer.
You can use the following css pattern with querySelectorAll .competition-rounds td:nth-child(4) > a. Loop the returned nodeList and extract the href from each node. This selects for the 4th column within that table, then the child a tag, from which the href attribute is extracted during the loop.
Required references:
Microsoft Internet Controls
Microsoft HTML Object Library
Option Explicit
Public Sub PrintLinks()
Dim ie As SHDocVw.InternetExplorer, nodeList As MSHTML.IHTMLDOMChildrenCollection
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.academiadasapostas.com/stats/competition/brasil/26"
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set nodeList = ie.Document.querySelectorAll(".competition-rounds td:nth-child(4) > a")
Dim i As Long
For i = 0 To nodeList.length - 1
Debug.Print nodeList.Item(i).href
Next
Stop
.Quit
End With
End Sub
Reading:
:nth-child()
Child combinator
If you wish to get rid of IE, the following should do it:
Sub GetTragetLinks()
Const Url = "https://www.academiadasapostas.com/stats/competition/brasil/26"
Dim Html As HTMLDocument, I&
Set Html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", Url, False
.send
Html.body.innerHTML = .responseText
End With
With Html.querySelectorAll("tr[id*='gsm_id_'] td[title] > a")
For I = 0 To .Length - 1
Debug.Print .item(I).href
Next I
End With
End Sub
Reference to add:
Microsoft HTML Object Library

"object doesn't support property or method" VBA login

I'm trying to login in this website using VBA but no success so far.
Option Explicit
Sub logintest()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "www.renovigi.solar/"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("loginusr") ' This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Usernamae
Set HTMLInput = HTMLDoc.getElementById("loginpwd") 'This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Password
Set HTMLButton = HTMLDoc.getElementById("loginbtn") 'This is based on on your website
HTMLButton.Click
End Sub
I'm getting this error message.
"object doesn't support property or method"
You need to get the input elements and then set the values. also, you may try the alternative way which I have commented.
Sub logintest()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "www.renovigi.solar/"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("loginusr").Children(0)
'Set HTMLInput = HTMLDoc.querySelector("#loginusr input") ' This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Usernamae
Set HTMLInput = HTMLDoc.getElementById("loginpow").Children(0)
'Set HTMLInput = HTMLDoc.querySelector("#loginpow input") 'This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Password
Set HTMLButton = HTMLDoc.getElementById("loginbtn") 'This is based on on your website
HTMLButton.Click
End Sub

Where to find VBA functions for working with internet explorer

I am working on a VBA function to go to a webpage, find a single HTML element, and display its contents. Here is what I have so far.
Function WebTableToSheet(webpage As String, tag As String, count As Integer) As String
'Tested using IE7, Excel 2000 SP1, and Windows XP
Dim objIE As Object
Dim varTables, varTable, document
Dim allTags, tags
Dim varRows, varRow
Dim varCells, varCell
Dim lngRow As Long, lngColumn As Long
Dim strBuffer As String
Set objIE = CreateObject("InternetExplorer.Application")
'Setup
With objIE
.AddressBar = False
.StatusBar = False
.MenuBar = False
.Toolbar = 0
.Visible = True
.Navigate webpage
End With
'Load webpage
While objIE.Busy
Wend
While objIE.document.ReadyState <> "complete"
Wend
varTable = objIE.document.All
allTags = objIE.document.All.tags(tag)
WebTableToSheet = allTags.Item(count)
Debug.Print "Testing function"
Cleanup:
Set varCell = Nothing: Set varCells = Nothing
Set varRow = Nothing: Set varRows = Nothing
Set varTable = Nothing: Set varTables = Nothing
objIE.Quit
Set objIE = Nothing
End Function
I am able to open InternetExplorer, got to the webpage specified in the function, but when I try searching for a specific tag, it seems to fail. I have had trouble searching for Microsoft VBA Internet Explorer documentation and knew what variables and methods are available for this task?
I'd recommend setting a reference to shdocvw.dll and the Microsoft HTML Object Library. In XP shdocvw.dll is in your system32 folder, the HTML Object Library should be in your list of references. I only have access to XP at the moment, so you'll have to do a search for any other ver of windows you're using. Then in your code you can do the following:
Dim IE as SHDocVw.InternetExplorer
Dim doc as HTMLDocument
Dim el as IHTMLElement
Set IE = new SHDocVw.InternetExplorer
With IE
.navigate "some.webpage.com"
.visible = true
End With
Do
'Nothing
Loop Until IE.readyState = READYSTATE_COMPLETE ' = 4
Set doc = IE.Document
Set el = doc.getElementById("someElementId")
If Not el Is Nothing Then
MsgBox el.innerText
End If
Team,
IE webpage full loading check use below mentioned code
Application.Wait (Now + TimeValue("0:00:1"))
Do Until IE.Busy = False
DoEvents
Loop
ieApp.Navigate "Https://duckduckgo.com/"
'Wait for Internet Explorer to finish loading
Do While ieApp.Busy: DoEvents: Loop
Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
ieApp.Document.getelementbyid("search_form_input_homepage").Value = "Gajendra Santosh"
ieApp.Document.getelementbyid("search_button_homepage").Click

Resources