使用VBA在带有框架的网页上执行javascript

时间:2016-11-18 03:08:11

标签: javascript vba internet-explorer

我正在尝试在包含框架的IE 11网页上执行javascript,我正在使用VBA来执行javascript。这是我的第一次尝试,所以我可能会犯一些菜鸟错误。我试图自动化的动作是按下" +"在文件夹上签名以扩展它。我已经看过很多关于这个主题的讨论,我试图包括我认为与我的代码相关的内容。但经过多次尝试,我仍然无法让它发挥作用。我试图开始工作的VBA代码如下。以下每次调用execScript都是尝试执行javascript。我收到的VBA错误消息在每次调用之前都作为注释放置。 提前感谢您提供的任何帮助。

import sqlite3
names = ['Tom', 'Dick', 'Harry']

ids = ['A452', 'B698', 'Kd9f']


conn = sqlite3.connect('testforinput.db')
c = conn.cursor()

c.execute("CREATE TABLE thetable(name TEXT, id TEXT)")

index = 0
for link in names:
    idofperson = ids[index]
    c.execute("INSERT INTO thetable(name, id)VALUES(?, ?)", ( [link], idofperson ))
    index+=1
conn.commit()

conn.close()

以下是我可以使用Firebug抓取的框架HTML的一部分:

    Dim doc             As MSHTML.IHTMLDocument
    Dim Frames          As MSHTML.IHTMLFramesCollection2
    Dim Window          As MSHTML.IHTMLWindow2

    ie.Navigate "URL of my web page"
    Set doc = ie.Document


    ' Error: Method 'frames' of object JScriptTypeInfo failed.

    Call ie.Document.Frames(0).execScript("populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');", "JavaScript")

    ' Error: Could not complete the operation due to error 80020101.

    Call ie.Document.Frames.Item(0).execScript("populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');", "JavaScript")

    ' Error: Method 'frames' of object JScriptTypeInfo failed.

    Call ie.Document.Frames("navigationBodyFrame").execScript("populateNode(document.getElementById('treeViewRootNode_Home_0_children'),treeView_Data,document.getElementById('treeViewRootNode_Home_0'),'treeViewRootNode_Home_0');", "JavaScript")

1 个答案:

答案 0 :(得分:1)

我今天能够正常工作。这是我做的,可能有一个更简单的方法。如果有,我有兴趣听到它。

使用VBA调试器,我能够发现我的网页中有多个级别的帧。 使用firebug,我能够找出哪个框架包含我想要按的图标。 这是按下我需要按下的图标的代码。

Dim ie             As SHDocVw.InternetExplorer
Dim doc            As MSHTML.HTMLDocument
Dim Frames         As MSHTML.IHTMLFramesCollection2
Dim Window         As MSHTML.IHTMLWindow2

ie.Navigate "URL of my web page"
Set doc = ie.Document
Set Window = doc.Frames(0).Frames(0).Frames(1).Frames(0)
Call Window.execScript("collapseHeader();", "JavaScript")

Set doc = Nothing
Set Window = Nothing