无法以有效的方式切换到新选项卡
Unable to switch to a new tab in an efficient manner
我在vba写了一个脚本,可以点击网页的某个link(Draw a map
)。单击完成后,将打开一个新选项卡,其中包含我想从中获取的信息。我的脚本可以无误地完成所有这些工作。在 运行 脚本上,它从新选项卡中抓取可见的标题 Make a Google Map from a GPS file
。
我的问题:除了使用像 If IE.LocationURL Like "*" & "output_geocoder" Then
这样的硬编码搜索之外,还有其他方法可以切换到新标签页吗?
这是我的脚本:
Sub FetchInfo()
Const url As String = "http://www.gpsvisualizer.com/geocoder/"
Dim IE As New InternetExplorer, Html As HTMLDocument, R&
Dim winShell As New Shell
With IE
.Visible = True
.navigate url
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Html.querySelector("input[value$='map']").Click
For Each IE In winShell.Windows
If IE.LocationURL Like "*" & "output_geocoder" Then
IE.Visible = True
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
Set Html = IE.document
Exit For
End If
Next
Set post = Html.querySelector("h1")
MsgBox post.innerText
IE.Quit
End Sub
要执行上述脚本,请将此引用添加到库中:
Microsoft Shell Controls And Automation
Microsoft Internet Controls
Microsoft HTML Object Library
顺便说一句,上面的脚本没有任何问题。我只想知道有什么更好的方法来做同样的事情。
这是迄今为止我使用 selenium 的最好结果
Option Explicit
Public Sub GetInfo()
Dim d As WebDriver
Set d = New ChromeDriver
Const url = "http://www.gpsvisualizer.com/geocoder/"
With d
.Start "Chrome"
.get url
.FindElementByCss("input[value$='map']").Click
.SwitchToNextWindow
.FindElementByCss("input.gpsv_submit").Click
MsgBox .Title
Stop
.Quit
End With
End Sub
更固定的标题是:
.SwitchToWindowByTitle("GPS Visualizer: Draw a map from a GPS data file").Activate
.FindElementByCss("input.gpsv_submit").Click
tl;dr;
我需要阅读更多关于 .SwitchToNextWindow
有多强大的信息。
仅供参考,您可以通过以下方式获取句柄信息:
Dim hwnds As List
Set hwnds = driver.Send("GET", "/window_handles")
我在vba写了一个脚本,可以点击网页的某个link(Draw a map
)。单击完成后,将打开一个新选项卡,其中包含我想从中获取的信息。我的脚本可以无误地完成所有这些工作。在 运行 脚本上,它从新选项卡中抓取可见的标题 Make a Google Map from a GPS file
。
我的问题:除了使用像 If IE.LocationURL Like "*" & "output_geocoder" Then
这样的硬编码搜索之外,还有其他方法可以切换到新标签页吗?
这是我的脚本:
Sub FetchInfo()
Const url As String = "http://www.gpsvisualizer.com/geocoder/"
Dim IE As New InternetExplorer, Html As HTMLDocument, R&
Dim winShell As New Shell
With IE
.Visible = True
.navigate url
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Html.querySelector("input[value$='map']").Click
For Each IE In winShell.Windows
If IE.LocationURL Like "*" & "output_geocoder" Then
IE.Visible = True
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
Set Html = IE.document
Exit For
End If
Next
Set post = Html.querySelector("h1")
MsgBox post.innerText
IE.Quit
End Sub
要执行上述脚本,请将此引用添加到库中:
Microsoft Shell Controls And Automation
Microsoft Internet Controls
Microsoft HTML Object Library
顺便说一句,上面的脚本没有任何问题。我只想知道有什么更好的方法来做同样的事情。
这是迄今为止我使用 selenium 的最好结果
Option Explicit
Public Sub GetInfo()
Dim d As WebDriver
Set d = New ChromeDriver
Const url = "http://www.gpsvisualizer.com/geocoder/"
With d
.Start "Chrome"
.get url
.FindElementByCss("input[value$='map']").Click
.SwitchToNextWindow
.FindElementByCss("input.gpsv_submit").Click
MsgBox .Title
Stop
.Quit
End With
End Sub
更固定的标题是:
.SwitchToWindowByTitle("GPS Visualizer: Draw a map from a GPS data file").Activate
.FindElementByCss("input.gpsv_submit").Click
tl;dr;
我需要阅读更多关于 .SwitchToNextWindow
有多强大的信息。
仅供参考,您可以通过以下方式获取句柄信息:
Dim hwnds As List
Set hwnds = driver.Send("GET", "/window_handles")