gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:53:40 26 4
gpt4 key购买 nike

我正在尝试根据 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com