excel - 在excel VBA中通过 Selenium 为每个谷歌搜索创建新标签

标签 excel vba selenium web-scraping

我正在尝试根据 Sheet1 中 A 列中的一些数据进行谷歌搜索 .. 我需要在新选项卡中打开每个单元格内容并搜索该单元格
示例:A1 有“花”这个词,所以我希望创建一个选项卡并导航到谷歌搜索那个“花”,然后搜索下一个单元格,依此类推
并且每次搜索都在一个新标签中
这是我的尝试

Sub Test()
Dim bot         As New ChromeDriver
Dim Keys        As New Keys

bot.Get "https://www.google.com"
'search for items in column A

bot.ExecuteScript "window.open(arguments[0])", "https://www.google.com"
bot.SwitchToNextWindow
End Sub

我也试过那部分
bot.FindElementById("gsr").SendKeys Range("A1").Value
bot.SendKeys bot.Keys.Enter
bot.SwitchToNextWindow

但我无法创建新标签

最佳答案

试试下面的。您需要针对文本输入的搜索框。

Option Explicit
Public 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 later
End Sub

使用定时循环查找元素:
Option Explicit
Public 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 later
End Sub

关于excel - 在excel VBA中通过 Selenium 为每个谷歌搜索创建新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53381891/

相关文章:

excel - 如何防止 Excel 在宏计算时渲染电子表格?

Excel 用连续 x 轴绘制时间序列频率

java - 使用 Applet 和 Apache POI 打开 Excel 文件

excel - 循环遍历范围内的行,并根据该行中的单元格将条件格式应用于整列

excel - 从 VBA 中的函数返回数组

excel - 使用 "Cells"引用的范围引用

python - 我真的被 Python selenium 模块困住了

java - 单击按钮直到 Selenium 中出现警报

python - 如何使用 Python win32com 调用 Excel VBA 函数和子函数?

c# - TransactionScope 回滚可以与 Selenium 或 Watin 一起使用吗?