在excel VBA中通过selenium为每个google搜索创建新选项卡
我正试图根据表1 A列中的一些数据进行谷歌搜索。。我需要在新选项卡中打开每个单元格内容,并搜索该单元格 示例:A1有一个单词“flower”,因此我希望创建一个选项卡,然后导航到google,搜索该“flower”,然后搜索下一个单元格,依此类推 并且每个搜索都将位于一个新选项卡中 这是我的尝试在excel VBA中通过selenium为每个google搜索创建新选项卡,excel,vba,selenium,web-scraping,Excel,Vba,Selenium,Web Scraping,我正试图根据表1 A列中的一些数据进行谷歌搜索。。我需要在新选项卡中打开每个单元格内容,并搜索该单元格 示例:A1有一个单词“flower”,因此我希望创建一个选项卡,然后导航到google,搜索该“flower”,然后搜索下一个单元格,依此类推 并且每个搜索都将位于一个新选项卡中 这是我的尝试 Sub Test() Dim bot As New ChromeDriver Dim Keys As New Keys bot.Get "https://www.goog
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
选项显式
公共子测试()
Dim bot作为ChromeDriver,键作为新键,arr(),ws作为工作表,i作为长
设置bot=新的色度驱动器
Set ws=thispoolk.Worksheets(“Sheet1”)“非常感谢QHarr先生。我在这一行.FindElementByCss(“[title=Search]”)遇到错误。SendKeys arr(I)
。。没有为我的CSSWorks找到元素,但您可能需要一个循环来显示它。我现在要写一些东西和加载项。您能否确认在您打开的本地版本中,[title=Search]可以正常工作。选择搜索框并右键单击inspect元素,然后检查它是否具有值为search的属性标题。您的本地版本可能有不同之处。在这种情况下,请共享该元素的HTML。我使用了这一行bot.SendKeys bot.keys。在SendKeys之后输入,效果很好。非常感谢你。你是个传奇人物别担心。很抱歉,我不知道您是否只是想输入值或启动搜索,这就是为什么我只是作为输入离开的原因。
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