首页 > 解决方案 > 动态站点上的 VBA Web 自动化

问题描述

我正在尝试从该站点删除条形码所有者:http: //gepir.gs1.org/index.php/search-by-gtin

由于最后一行,我收到“对象不支持此属性或方法”错误消息。我试图用类和标签提取它,但失败了。

我正在尝试从动态表中提取公司名称。

Sub aa()

Dim ie As New SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Dim aaa As Object
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer

ie.Visible = True
ie.navigate "gepir.gs1.org/index.php/search-by-gtin"

Do While ie.readyState <> READYSTATE_COMPLETE
Loop

Debug.Print ie.LocationName, ie.LocationURL

Set doc = ie.document

ie.document.forms("searchbygtin").elements("keyValue").Value = "685387379712"
ie.document.forms("searchbygtin").elements("method").Click

Debug.Print ie.document.getElementsBytag("tr")(7).innerText

End Sub

任何帮助都会很棒,谢谢

标签: htmlexcelvbaweb-scrapingwebautomation

解决方案


你需要标签名

Debug.Print ie.document.getElementsByTagName("tr")(7).innerText

我得到了一个验证码,所以我怀疑你是否可以通过这种方式完成这项任务。Selenium 和 IE 似乎都触发了验证码,至少对我来说是这样。


如果不是验证码,我会按如下方式重写您的脚本(可能使用循环来确保存在 tr 元素):

Option Explicit

Public Sub GetLastChangeDate()
    Dim ie As New SHDocVw.InternetExplorer, doc As HTMLDocument
    Const URL = "gepir.gs1.org/index.php/search-by-gtin"

    With ie
        .Visible = True
        .navigate URL

        While .Busy Or .readyState < 4: DoEvents: Wend

        Set doc = .document

        Debug.Print .LocationName, .LocationURL

        doc.getElementById("keyValue").Value = "685387379712"
        doc.getElementById("submit-button").Click

        While .Busy Or .readyState < 4: DoEvents: Wend

        Debug.Print .document.getElementsByTagName("tr")(7).innerText
    End With
End Sub

推荐阅读