使用VBA通过Access导航网页/ maniuplate IE [英] Using VBA to navigate web page/ maniuplate IE via Access

查看:182
本文介绍了使用VBA通过Access导航网页/ maniuplate IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,StackOverflow社区,



我有一个关于使用Access VBA来操纵IE的问题。 ,我试图编写代码来使用IE打开特定的网页,在特定的链接上搜索该页面(目标链接的名称将取决于用户的情况),通过编程方式点击该链接导航到新页面,然后通过在生成的新页面上查找特定的链接/元素来重复该过程。



最终目标链接的显示文本将始终相同,但它位于的页面在每种情况下都会有所不同。



我的问题是以编程方式在二级页面中搜索驻留在那里的元素...我的结果即使在浏览器加载了新的链接之后,我仍然只给予我第一层页面的元素。



如果我描述的是到目前为止的问题。



我的c本质上,如下所示:

  Dim ie As Object,ieDoc As HTMLDocument 

Set ie = CreateObject(InternetExplorer.Application)
ie.Visible = True

strHTML =http://targetsite.com/first-level_page

ie .navigate strHTML

'等待浏览器
虽然ie.ReadyState<> READYSTATE_COMPLETE
DoEvents
Wend


'定义第一级目标链接并搜索它,然后点击它

Dim i As Integer
Dim txt As String,link As String,p As String

Set ieDoc = ie.Document

txt =一级目标
Do直到link = txt或i> = 1000
i = i + 1
link = ieDoc.Links(i).outerText
Loop

'I知道上述循环在当前形式上并不完美,但它确实为我尝试构建的功能提供了第一次尝试。

ieDoc.Links(i).Click

到目前为止,这么好。上面的代码正如预期的那样工作。在所有情况下,它都能正确导航到所需的二级页面。当我尝试在最终目标元素的二级页面中搜索时,我会出错的是:

 'wait for浏览器
虽然ie.ReadyState<> READYSTATE_COMPLETE
DoEvents
Wend

'搜索最终目标元素(在所有二级页面上始终具有相同的名称和锚文本)
ieDoc.getElementsByName( item.Click)

上面的行(基于.getElementsByName)如果我尝试在第一级页面上使用它,则工作得很好。但是,一旦我导航到任何二级页面,它什么都不做。我也尝试用上面的循环的修改版本替换它,该循环搜索第一级页面上的链接。同样的结果。

作为故障排除步骤,我还用debug.print命令替换了该行,以简单命名第二级页面上的所有元素,而这总是返回来自第一页的元素名称。



我认为很明显,我无法正确更新或重新定义我的HTML文档后,我已遵循第一个环节。我将ieDoc设置为无,然后将其重新设置为ie.document,但这也不起作用。



希望有一个简单的命令或语法我完全无知。但对于我的生活,我还没有能够让VBA在第一个链接被点击后正确地引用页面上存在的HTML元素。



预先感谢任何人建议!

〜JQN

解决方案

您需要使用组合@Matteo NNZ和@TimWilliams



如果您知道元素的id名称,则更易于使用getElementsByName(target)。



如果您不知道id,则循环锚元素并搜索正确的文本。

  Set Anchors = IeDoc.getElementsByTagName(a)

code>

然后循环比较.outertext或任何您需要的子字段。

@TimWilliams:
您需要加载找到的URL,然后在循环内设置新加载的IeDoc。将ie对象设置为新的ie加载页面,否则ie对象将正确保持加载的第一页。
当你找到你的链接时,你需要像你已经完成的那样加载新页面。

 ''扔掉加载的第一页。 
ie = nothing
IeDoc = nothing

''设置加载的新页面。
ie.navigate newHTML

'等待浏览器
虽然ie.ReadyState< READYSTATE_COMPLETE
DoEvents
Wend

Set IeDoc = ie.Document

为第二页重复上述过程

以下是有关'在vba中解析HTML'的其他资源:
http://www.ozgrid.com/forum/showthread.php?t=184695
解析VBA中的HTML内容


Hello StackOverflow community,

I have a question about using Access VBA to manipulate IE.

Essentially, I am trying to write code that will open a specific webpage using IE, search that page for a particular link (the name of the target link will depend on the circumstances of the user), navigate to a new page by programmatically clicking that link, and then repeating the process by looking for a specific link/element on the resulting new page.

The display text for the ultimate target link will always be the same, but the page that it resides on will be different in every case.

My issue is with programmatically searching the second-level page for the elements that reside there...my results keep giving me only elements from the first-level page, even after the browser has loaded the new link.

Apologies if I'm doing a poor job describing the context of the question so far.

My code, essentially, is as follows:

Dim ie As Object, ieDoc As HTMLDocument

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True

strHTML = "http://targetsite.com/first-level_page"

ie.navigate strHTML

'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend


'define first-level target link and search for it and then "click" it

Dim i As Integer
Dim txt As String, link As String, p As String

Set ieDoc = ie.Document

txt = "First-Level Target"
    Do Until link = txt Or i >= 1000
    i = i + 1
    link = ieDoc.Links(i).outerText
Loop

'I know the above loop is not exactly ideal in its current form, but it does give me a working first attempt at the functionality I'm trying to build.    

ieDoc.Links(i).Click

So far, so good. The code above works just as intended. It navigates correctly to the desired second-level page in all cases. Where I'm going wrong is when I try to search that second-level page for the final target element:

'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend

'Search for final target element (which always has the same name and anchor text on all second-level pages)
ieDoc.getElementsByName("final-target-name").Item.Click

The line above (based on .getElementsByName) works just fine if I try to use it on the first-level page. But it does nothing once I've navigated to any second-level page. I've also tried to replace it with a modified version of the loop above that searches for the link on the first-level page. Same result.

As a troubleshooting step, I've also replaced that line with a debug.print command to simply name all of the elements on the second-level page, and this always returns the element names from the first page instead.

I assume it's clear that I'm somehow failing to correctly update or redefine my HTMLdocument after I've followed the first link. I've setting ieDoc as nothing and then re-setting it as ie.document, but that doesn't work either.

Hopefully there is a simple command or syntax that I'm simply ignorant to. But for the life of me I havent been able to have VBA correctly refer to the HTML elements existing on the page after the first link is clicked.

Thanks in advance for any advice!

~JQN

解决方案

You need to use a combination of @Matteo NNZ and @TimWilliams

If you know the "id" name of the element its easier to use getElementsByName("target").

if you don't know the "id" then loop anchor elements and search for the correct text. This narrows down to only Link or elements.

Set Anchors = IeDoc.getElementsByTagName("a")

then loop comparing .outertext or whatever sub field you need.

@TimWilliams: You need to load the found URL then set the newly loaded IeDoc inside the loop. Set the "ie" object to the new "ie" loaded page otherwise the 'ie' object will correctly remain the first page loaded. When you find your link you need to load the new page as you have already done.

'' throw away the first page loaded.
ie = nothing 
IeDoc = nothing

'' Set the new page loaded.
ie.navigate newHTML

'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend

Set IeDoc = ie.Document

Repeat the process as above for the second page

Here are some other resources about 'parsing HTML in vba': http://www.ozgrid.com/forum/showthread.php?t=184695 Parse HTML content in VBA

这篇关于使用VBA通过Access导航网页/ maniuplate IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆