Create new tab for each google search by selenium in excel VBA Create new tab for each google search by selenium in excel VBA selenium selenium

Create new tab for each google search by selenium in excel VBA


Try the following. You need to target the search box for text input.

Option ExplicitPublic Sub Test()    Dim bot As ChromeDriver, keys As New keys, arr(), ws As Worksheet, i As Long    Set bot = New ChromeDriver    Set ws = ThisWorkbook.Worksheets("Sheet1") '<==Adjust to your sheet    arr = Application.Transpose(ws.Range("A1:A3")) '<== Adjust to your range    With bot        .Start "Chrome"        .get "https://google.com/"        For i = LBound(arr) To UBound(arr)            If Not IsEmpty(arr(i)) Then                If i > 1 Then                    .ExecuteScript "window.open(arguments[0])", "https://google.com/"                    .SwitchToNextWindow                End If                .FindElementByCss("[title=Search]").SendKeys arr(i)            End If        Next    End With    Stop '<==Delete me laterEnd Sub

Using a timed loop to find the element:

Option ExplicitPublic Sub Test()    Dim bot As ChromeDriver, keys As New keys, arr(), ws As Worksheet, i As Long    Const MAX_WAIT_SEC As Long = 5    Dim ele As Object, t As Date    Set bot = New ChromeDriver    Set ws = ThisWorkbook.Worksheets("Sheet1")   '<==Adjust to your sheet    arr = Application.Transpose(ws.Range("A1:A3")) '<== Adjust to your range    With bot        .Start "Chrome"        .get "https://google.com/"        For i = LBound(arr) To UBound(arr)            If Not IsEmpty(arr(i)) Then                If i > 1 Then                    .ExecuteScript "window.open(arguments[0])", "https://google.com/"                    .SwitchToNextWindow                End If                t = Timer                Do                    DoEvents                    On Error Resume Next                    Set ele = .FindElementByCss("[title=Search]")                    On Error GoTo 0                    If Timer - t > MAX_WAIT_SEC Then Exit Do                Loop While ele Is Nothing                If Not ele Is Nothing Then                    ele.SendKeys arr(i)                Else                    Exit Sub                End If            End If        Next    End With    Stop                                         '<==Delete me laterEnd Sub