Intro> I'm new to vba for ie, I've been searching in many forums for days, but I still couldn't find someone with the same problem as me. That's why I'm creating this new thread. Would someone please help?
My goal> I want to automate my searches for flight tickets in skyscanner . com
More details> Basically I want to give in the origin, destination and dates, and click on search and collect the cheapest flight information back to my excel file.
My problem> I could successfully input origin, destination and flight dates, and I was able to click on 'search', BUT the inputs I had given (origin, destination, dates) were totally ignored by the browser, resulting in an empty search with random dates. It seems like my inputs were ignored..
My Excel looks like this>
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Origin[/TD]
[TD]Destination[/TD]
[TD]Depart[/TD]
[TD]Return[/TD]
[TD]Cheapest Flight[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Hanoi (VAN)[/TD]
[TD]Budapest (BUD)[/TD]
[TD]15.06.2018[/TD]
[TD]30.06.2018[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Cologne (CGN)[/TD]
[TD]Budapest (BUD)[/TD]
[TD]15.06.2018[/TD]
[TD]30.06.2018[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Here is my code>
Sub SearchCheapFlights()
Range("E2:F5").ClearContents
Dim ie As Object
Dim form As Object
Dim LR As Integer
Dim var1 As String
Dim var2 As String
Dim var3 As String
Dim var4 As String
Dim varResult As Object
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var1 = Cells(x, 1).Value
var2 = Cells(x, 2).Value
var3 = Cells(x, 3).Value
var4 = Cells(x, 4).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "http://www.skyscanner.com"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("js-origin-input").Value = var1
ie.document.getElementById("js-destination-input").Value = var2
ie.document.getElementById("js-depart-input").Value = var3
ie.document.getElementById("js-return-input").Value = var4
'HERE IS WHERE THE PROBLEM OCCURS
Set form = ie.document.getElementsByClassName("fss-bpk-button fss-bpk-button--large js-search-button")
Application.Wait (Now + TimeValue("0:00:02"))
form(0).Click
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
Set varResult = ie.document.getElementByClass("fqs-price")
Cells(x, 5).Value = varResult.innerText
ie.Quit
Set ie = Nothing
Next x
End Sub
My goal> I want to automate my searches for flight tickets in skyscanner . com
More details> Basically I want to give in the origin, destination and dates, and click on search and collect the cheapest flight information back to my excel file.
My problem> I could successfully input origin, destination and flight dates, and I was able to click on 'search', BUT the inputs I had given (origin, destination, dates) were totally ignored by the browser, resulting in an empty search with random dates. It seems like my inputs were ignored..
My Excel looks like this>
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Origin[/TD]
[TD]Destination[/TD]
[TD]Depart[/TD]
[TD]Return[/TD]
[TD]Cheapest Flight[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Hanoi (VAN)[/TD]
[TD]Budapest (BUD)[/TD]
[TD]15.06.2018[/TD]
[TD]30.06.2018[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Cologne (CGN)[/TD]
[TD]Budapest (BUD)[/TD]
[TD]15.06.2018[/TD]
[TD]30.06.2018[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Here is my code>
Sub SearchCheapFlights()
Range("E2:F5").ClearContents
Dim ie As Object
Dim form As Object
Dim LR As Integer
Dim var1 As String
Dim var2 As String
Dim var3 As String
Dim var4 As String
Dim varResult As Object
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var1 = Cells(x, 1).Value
var2 = Cells(x, 2).Value
var3 = Cells(x, 3).Value
var4 = Cells(x, 4).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "http://www.skyscanner.com"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("js-origin-input").Value = var1
ie.document.getElementById("js-destination-input").Value = var2
ie.document.getElementById("js-depart-input").Value = var3
ie.document.getElementById("js-return-input").Value = var4
'HERE IS WHERE THE PROBLEM OCCURS
Set form = ie.document.getElementsByClassName("fss-bpk-button fss-bpk-button--large js-search-button")
Application.Wait (Now + TimeValue("0:00:02"))
form(0).Click
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
Set varResult = ie.document.getElementByClass("fqs-price")
Cells(x, 5).Value = varResult.innerText
ie.Quit
Set ie = Nothing
Next x
End Sub