Select button by looping through all innerHTML - vba

I am trying to get all the buttons using queryselector and select the particular button by looping through and finding innerHTML.
After finding the particular button, when click event is passed, nothing happens.
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.spirit.com/"
Do Until IE.readyState = 4: Application.Cursor = xlDefault: DoEvents: Loop
Application.Wait (Now + TimeValue("00:00:03"))
IE.document.querySelector("label[for='radio-oneWay']").Click
Application.Wait (Now + TimeValue("00:00:03"))
IE.document.querySelector("input[id='flight-OriginStationCode']").Click
Set elems = IE.document.getElementsByTagName("button")
For Each elem In elems
If (elem.innerHTML) = " Aguadilla, Puerto Rico (BQN) " Then
elem.Click
Exit For
End If
Next elem

The two airport boxes can be assigned by simulating input using SendKeys then the click event will be triggered. The date-input and other textbox will be set separately. Here is the code which implements this function:
Private Sub CommandButton1_Click()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.spirit.com/"
Do Until IE.readyState = 4: Application.Cursor = xlDefault: DoEvents: Loop
Application.Wait (Now + TimeValue("00:00:03"))
IE.document.querySelector("label[for='radio-oneWay']").Click
Application.Wait (Now + TimeValue("00:00:03"))
Set elems = IE.document.getElementsByTagName("button")
Set txt1 = IE.document.getElementsByName("originStationCode")(0)
txt1.Focus
SendKeys ("BQN")
Application.Wait (Now + TimeValue("00:00:01"))
Set txt2 = IE.document.querySelector("input[id='flight-DestinationStationCode']")
txt2.Focus
SendKeys ("BDL")
Application.Wait (Now + TimeValue("00:00:01"))
Application.Wait (Now + TimeValue("00:00:01"))
IE.document.querySelector("input[id='flight-Date']").Value = "2020/10/09-2020/10/19"
IE.document.querySelector("input[id='promoCode']").Value = "xx"
IE.document.querySelector("button[type='submit']").Click
End Sub
Result:

Related

Application.wait in userform -duplicate-

so I am trying to 'animate' a userform so that the text in a label changes over time. here is a sample of my code (it's run upon clicking a button in the userform):
Label2.Caption = "hello"
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "10"
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "9"
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "8"
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "7"
This should open with hello, then count from 10 to 7 with 1-second intervals.
When I run the code, the label changes to hello, then freezes for about 30 seconds, then, shows the last number. this problem does not happen if I step through the code with the debugger.
I've shown that Application.wait works on its own with this code (in a normal sub):
cells(1,1) = "hi"
Application.Wait (Now + TimeValue("0:00:01"))
cells(1,1) = "bye"
Application.Wait (Now + TimeValue("0:00:01"))
cells(1,1) = "hi"
how do I fix this and why is it broken?
EDIT:
This question is a duplicate. See this question
add .Repaint between changes. so my new code is:
Label2.Caption = "hello"
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "10"
myForum.Repaint
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "9"
myForum.Repaint
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "8"
myForum.Repaint
Application.Wait (Now + TimeValue("0:00:01"))
Label2.Caption = "7"
myForum.Repaint

Next File in the DIR does not open

All my sendkeys execute properly and the document saves and closes but then the next document does not Open. Before I added the sendkeys, the documents in the DIR would each open but now the next document will not. What am I doing wrong?
Sub Password()
Dim CustRow, LastRow As Long
Dim Password As String
Dim fileName As Variant
With Sheet1
LastRow = .Range("C9999").End(xlUp).Row
fileName = Dir("C:\State_K-1_Info\Password\*.pdf")
Do While fileName <> ""
CreateObject("Shell.Application").Open ("C:\State_K-1_Info\Password\" & fileName)
Application.Wait Now + 0.00005
For CustRow = 2 To LastRow
Password = .Range("C" & CustRow).Value
Application.SendKeys "{F6}", True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.Wait Now + 0.00001
Application.SendKeys Password, True
Application.Wait Now + 0.00002
Application.SendKeys "(~)", True
Application.Wait Now + 0.00001
Application.SendKeys Password, True
Application.Wait Now + 0.00002
Application.SendKeys "(~)", True
Application.Wait Now + 0.00001
Application.SendKeys "(~)", True
Application.Wait Now + 0.00001
Application.SendKeys "^(s)", True
Application.Wait Now + 0.00001
Application.SendKeys "%{F4}", True
Application.Wait Now + 0.00001
fileName = Dir
Next CustRow
Loop
End With
End Sub

Is there a For...Next loop to jump between web pages to scrape?

So I have been trying to scrape info from a site using the code below but am having a tough time with the part I noted. Is there an easier way to get the results I want rather than just repeating the same code over and over? You never know how many results you will get so I cannot use that info.
Dim objIE As InternetExplorer
Dim y As Integer
Dim result As String
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.searchiqs.com/nyalb/Login.aspx"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementById("btnGuestLogin").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementById("ContentPlaceHolder1_txtFromDate").Value = "07/01/2017"
objIE.document.getElementById("ContentPlaceHolder1_txtThruDate").Value = "07/19/2017"
objIE.document.getElementById("ContentPlaceHolder1_cboDocGroup").Value = "DBA"
objIE.document.getElementById("ContentPlaceHolder1_cmdSearch").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
' brings up a table of results. I need to click on the view button and get the info from that page
objIE.document.getElementById("ContentPlaceHolder1_grdResults_btnView_0").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
' element ID's are hidden for those looking to help
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
' I would love to direct link to the next serch result here but the link does not change between pages so I click the next button
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
' and I click the next button again
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
'AND AGAIN
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
y = y + 1
' You get the idea
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
result = Trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
' I basically repeat that process til I cant press CTRL V anymore. Is there a loop I can use to tighten this up?
Let me know. You guys have been a huge help in the past. Also would it be beneficial to use the XML method as my readystate loops have been giving me issues? Thanks in advance.
Yep, a For loop for the required number of iterations if it's a static set of pages. I believe we can make it simpler.
For y = 1 to 5
result =trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
next y
'or if you want it to keep going, something like this
Do until objIE.document.getElementById("ContentPlaceHolder1_btnNext") is nothing
result =trim(objIE.document.getElementById("ContentPlaceHolder1_lblDetails2").innerText)
Sheets("Sheet1").Range("C" & y).Value = result
objIE.document.getElementById("ContentPlaceHolder1_btnNext").Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
loop

Scraping 'span' tag not working

I'm having trouble getting to the span tag and grabbing the inner text for "1-Day Total Return".
Here is the webpage:
http://www.morningstar.com/funds/XNAS/DODFX/quote.html
Here is my code
Sub Macro1()
'
link = "http://www.morningstar.com/funds/XNAS/DODFX/quote.html"
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate link
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = ie.document
While ie.readyState <> 4
Wend
On Error Resume Next
Application.Wait (Now + TimeValue("00:00:02"))
range("B5").Offset(0, 0).Value = doc.getElementById("msqt_summary")(0).getElementsByClassName("gr_colm_a2b")(0).getElementsByTagName("span")(0).innerText
End With
ie.Quit
Application.EnableEvents = True
'
End Sub
This is the way you can get your data. The data is indeed within an iframe.
Sub Web_Data()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim elem As Object, post As Object
With IE
.Visible = False
.navigate "http://www.morningstar.com/funds/XNAS/DODFX/quote.html"
Do While .readyState <> READYSTATE_COMPLETE: Loop
Set elem = .document.getElementById("quote_quicktake").getElementsByTagName("iframe")(0)
.navigate elem.src
While .readyState < 4: DoEvents: Wend
Set html = .document
End With
Set post = html.getElementsByClassName("gr_text_bigprice")(1).getElementsByTagName("span")(1)
MsgBox post.innerText
IE.Quit
End Sub
Output:
-0.67

VBA macro excel ..error not able to fetch information from the page

Not able to fetch data
Sub differntIEsize()
Dim ie As New SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate "https://en.wikipedia.org/wiki/Main_Page"
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
Set searchtxt = .document.getElementById("searchInput")
searchtxt.Value = "Earth"
.document.forms(0).submit
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
'IE.navigate "javascript:document.getElementById('.document.forms(0).submit').click();"
ThisWorkbook.Sheets(1).Range("A2") = "hgvgjjlj"
Set doc = .document
ThisWorkbook.Sheets(1).Range("A1").Value = doc.getElementById("toctitle").innerText
.Quit
End With
End Sub