I use selenium + vba to launch chrome to open 10 urls listed in cells range("A1:A10"). I am not familiar with selenium, after trying a lot of times I finally come out below clunky codes.
Private selenium As New ChromeDriver
Sub test()
Dim cell As Range
Dim keys As New selenium.keys
Dim pageNo As Integer
pageNo = 0
selenium.Start "chrome", "http://www.google.com/"
For Each cell In Range("A1:A10")
If pageNo >= 1 Then
selenium.SendKeys keys.Control & "t" + keys.Control & "t"
selenium.SwitchToNextWindow
End If
selenium.Get cell.Value
pageNo = pageNo + 1
Next
End Sub
Several troubles and questions raised:
I am confused why I have to send 2 times of "Ctrl+t" keys to successfully open new tab.
Why that I have to add selenium.SwitchToNextWindow after opening new tab, or the next url will be opened on the original tab, not new tab?
Quite often after opening 3~5 urls, vba pops up "not enough memory" error and stops loading the next url. But if I run the codes line by line in debugging environment, all 10 urls can be successfully opened. Why's that?
I googled that if I want chrome not to exit after finishing running macro, I have to declare object selenium outside the sub though I do not quite get the reason. Could anyone help explain me in plain way?
If any experts can help me optimize my codes, that would be much appreciated! My system is Win7 64bit + Excel 2010.
Thanks with best regards.