在 IE 中执行 javascript 时遇到问题
Trouble executing javascript within IE
我在 vba 中创建了一个脚本,使用 IE 来单击网页中的选项卡。我想知道如何使用 .execScript
.
在该选项卡上发起点击
当我像下面这样尝试时,它起作用了(不是理想的方法):
Sub ExecuteScript()
Dim IE As New InternetExplorer, Html As HTMLDocument
With IE
.Visible = True
.navigate "https://whosebug.com/questions/tagged/web-scraping"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
Html.parentWindow.execScript "document.querySelector(""a[href='/questions/ask']"").click();"
End With
End Sub
我想做的方式如下,这样我就可以使用对象变量(相邻或内部).execScript
:
Set post = Html.querySelector("a[href='/questions/ask']")
Html.parentWindow.execScript "arguments[0].click();", post
但是,它会抛出指向这一行的错误 Html.parentWindow.execScript
Run-time error `429`
ActiveX component can't create object
如何在IE中执行javascript?
为什么不更改变量类型,以便您可以将 post
作为字符串(即选择器)传递。然后你可以连接进去。
Option Explicit
Public Sub ExecuteAScript()
Dim IE As New InternetExplorer, Html As HTMLDocument, post As String, item As Object
post = "a[href='/questions/ask']"
With IE
.Visible = True
.navigate "https://whosebug.com/questions/tagged/web-scraping"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
Do
On Error Resume Next
Set item = .document.querySelector("" & post & "")
On Error GoTo 0
Loop While item Is Nothing
If Not item Is Nothing Then item.Click
Stop
End With
End Sub
如果你必须使用 execScript
我认为你不能像使用 selenium 那样使用 javascript return
调用传回值。您可以使用 js 向页面添加一个值,然后读回该值以便 return:
Option Explicit
Public Sub ExecuteAScript()
Dim IE As New InternetExplorer
post = "a[href='/questions/ask']"
With IE
.Visible = True
.navigate "https://whosebug.com/questions/tagged/web-scraping"
While .Busy Or .readyState < 4: DoEvents: Wend
Do
Call .document.parentWindow.execScript("document.title = document.querySelectorAll(" & Chr$(34) & post & Chr$(34) & ").length;")
Loop While .document.Title = 0
If IsNumeric(.document.Title) And .document.Title > 0 Then Debug.Print "Success"
End With
End Sub
我在 vba 中创建了一个脚本,使用 IE 来单击网页中的选项卡。我想知道如何使用 .execScript
.
当我像下面这样尝试时,它起作用了(不是理想的方法):
Sub ExecuteScript()
Dim IE As New InternetExplorer, Html As HTMLDocument
With IE
.Visible = True
.navigate "https://whosebug.com/questions/tagged/web-scraping"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
Html.parentWindow.execScript "document.querySelector(""a[href='/questions/ask']"").click();"
End With
End Sub
我想做的方式如下,这样我就可以使用对象变量(相邻或内部).execScript
:
Set post = Html.querySelector("a[href='/questions/ask']")
Html.parentWindow.execScript "arguments[0].click();", post
但是,它会抛出指向这一行的错误 Html.parentWindow.execScript
Run-time error `429`
ActiveX component can't create object
如何在IE中执行javascript?
为什么不更改变量类型,以便您可以将 post
作为字符串(即选择器)传递。然后你可以连接进去。
Option Explicit
Public Sub ExecuteAScript()
Dim IE As New InternetExplorer, Html As HTMLDocument, post As String, item As Object
post = "a[href='/questions/ask']"
With IE
.Visible = True
.navigate "https://whosebug.com/questions/tagged/web-scraping"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
Do
On Error Resume Next
Set item = .document.querySelector("" & post & "")
On Error GoTo 0
Loop While item Is Nothing
If Not item Is Nothing Then item.Click
Stop
End With
End Sub
如果你必须使用 execScript
我认为你不能像使用 selenium 那样使用 javascript return
调用传回值。您可以使用 js 向页面添加一个值,然后读回该值以便 return:
Option Explicit
Public Sub ExecuteAScript()
Dim IE As New InternetExplorer
post = "a[href='/questions/ask']"
With IE
.Visible = True
.navigate "https://whosebug.com/questions/tagged/web-scraping"
While .Busy Or .readyState < 4: DoEvents: Wend
Do
Call .document.parentWindow.execScript("document.title = document.querySelectorAll(" & Chr$(34) & post & Chr$(34) & ").length;")
Loop While .document.Title = 0
If IsNumeric(.document.Title) And .document.Title > 0 Then Debug.Print "Success"
End With
End Sub