简体   繁体   English

如何在Excel VBA Internet Explorer中使用通配符

[英]How to use wildcard in excel vba internet explorer

could you please tell me how to use wildcards in excel vba internet explorer? 您能告诉我如何在Excel VBA Internet Explorer中使用通配符吗?

id="btn_edit_card_1NLQNQD0D93O"

everytime that number in different. 每次那个数字都不一样。 How to click that button via 如何通过单击该按钮

 document.getElementById

There is no class in code 代码中没有类

 <a id="btn_edit_card_1NLQNQD0D93O" href="/trades/bejelentes_egyszerusitett/1NLQNQD0D93O">Editing basic data</a>

As @Nathan_Sav suggested, very wisely I might add, that you will have to work around your problem with collections. 正如@Nathan_Sav所建议的那样,我可能非常明智地补充道,您将不得不解决集合的问题。 For example, loop through all the "a" tags until you find one that "btn_edit_card_" as part of it's name. 例如,循环遍历所有“ a”标记,直到找到一个名称为“ btn_edit_card_”的标记。 This will work unless there are more than one "a" tags with that phrase in it. 除非有多个带有该短语的“ a”标签,否则这将起作用。

    Set els = ie.Document.getElementsByTagName("a")
    For Each el In els
         If el.ID Like "btn_edit_card*" 
            el.click
            Exit For
'           Debug.Print el.ID, el.Name
         End If  
    Next el

If your IE version is 9 or above you can use the querySelectorAll method of the HTMLDocument class. 如果您的IE版本是9或更高版本,则可以使用HTMLDocument类的querySelectorAll方法。

This uses CSS selectors to enable filtering of elements by their attributes. 这使用CSS选择器启用按元素属性过滤的元素。 In your case you are looking for a elements with an id beginning with btn_edit_card . 在您的情况下,您正在寻找一个idbtn_edit_card开头a元素。 The selector for this would be: 选择器为:

a[^=btn_edit_card]

Where the ^= means begins with . ^=意思是从开头

See the example code below that pulls comments from this very page - they are all tr elements in a table beneath your question which all have id of comment-123456 where the number can change from comment to comment (because they are stored uniquely in the database etc): 请参阅下面的示例代码,该代码从此页面提取注释-它们都是问题下方表格中的tr元素,它们的所有id均为comment-123456 ,其中的数字可以随注释的不同而改变(因为它们是唯一存储在数据库中的)等等):

Option Explicit

Sub GetElementByWildcard()

    Dim objIe As InternetExplorer
    Dim objDoc As HTMLDocument
    Dim objElements As IHTMLDOMChildrenCollection
    Dim objElement As Object
    Dim lngCounter As Long

    On Error GoTo ExitFunction

    'get page content
    Set objIe = New InternetExplorer
    objIe.Visible = False
    objIe.navigate "http://stackoverflow.com/questions/42225761/how-to-use-wildcard-in-excel-vba-internet-explorer"
    Do While objIe.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'get document
    Set objDoc = objIe.document

    'get any <tr> with an id starting with comment-
    Set objElements = objDoc.querySelectorAll("tr[id^=comment-]")

    'iterate output
    lngCounter = 0
    While lngCounter < objElements.Length
        Set objElement = objElements.Item(lngCounter)
        Debug.Print objElement.innerText
        lngCounter = lngCounter + 1
    Wend

ExitFunction:
    If Err.Number <> 0 Then
        Debug.Print Err.Description
    End If

    objIe.Quit
    Set objDoc = Nothing
    Set objIe = Nothing


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM