title: Selenium + VBA to Control Chrome
body: <p>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.</p>
  
  <pre><code>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 &gt;= 1 Then
              selenium.SendKeys keys.Control &amp; "t" + keys.Control &amp; "t"
              selenium.SwitchToNextWindow
          End If
          selenium.Get cell.Value
          pageNo = pageNo + 1
      Next
  End Sub
  </code></pre>
  
  <p>Several troubles and questions raised:</p>
  
  <ol>
  <li><p>I am confused why I have to send 2 times of "Ctrl+t" keys to successfully open new tab.</p></li>
  <li><p>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?</p></li>
  <li><p>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?</p></li>
  <li><p>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?</p></li>
  </ol>
  
  <p>If any experts can help me optimize my codes, that would be much appreciated! My system is Win7 64bit + Excel 2010.</p>
  
  <p>Thanks with best regards.</p>