【问题标题】:Use Excel VBA to click on a button in Internet Explorer, when the button has no "name" associated当按钮没有关联的“名称”时,使用 Excel VBA 单击 Internet Explorer 中的按钮
【发布时间】:2023-03-31 01:39:01
【问题描述】:

我正在尝试使用 excel 自动将值输入到时间表中。时间表在网页上。 现在我可以加载页面,输入我的用户名和密码,然后自己输入时间表。请参阅下面的代码。

此时我需要点击一个按钮来打开子表单。我无法提前知道要打开多少个子表单。我知道当一个按钮有“名字”时如何点击它。但在这种情况下,没有。因此,我在下面更新的代码使用循环打开所有其他子表单。第一次可以,但是当我再次这样做时

有人能告诉我如何确定页面中有多少个按钮以及如何点击每个按钮吗? 接下来,我将我现在拥有的代码放在它下面,我需要与之交互的页面的 HTML 代码。

Private Sub time_sheet_filling()

    Dim I As Long
    Dim IE As Object
    Dim doc As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.navigate "http://timesheet.cccc.ca/timesheet/"

    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

        'Load the logon page
    Set objCollection = IE.Document.getElementsByTagName("input")
    I = 0
    While I < objCollection.Length
        If objCollection(I).Name = "txtUserName" Then
            ' Set text to enter
            objCollection(I).Value = "6666"
        End If
        If objCollection(I).Name = "txtPwd" Then
            ' Set text for password
            objCollection(I).Value = "password"
        End If
        If objCollection(I).Type = "submit" And objCollection(I).Name = "btnSubmit" Then ' submit button clicking
            Set objElement = objCollection(I)
        End If
        I = I + 1
    Wend

    objElement.Click    ' click button to load the form

    ' Wait while IE re-loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    ' Show IE
    IE.Visible = True  
Dim links, link
Dim n, j
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length  
For j = 0 To n - 1 Step 2
    links(j).Click
'I have some operations to be done will post another question for this
IE.Document.getElementById"DetailToolbar1_lnkBtnSave").Click              'save
IE.Document.getElementById"DetailToolbar1_lnkBtnCancel").Click            'close
Next



End Sub  

下面是html代码的摘录。我正在尝试单击下面 html 代码最后一行中编码的按钮

<table width="984" class="Grid" id="dgTime" border="1" rules="all" cellspacing="0">
  <tbody>
    <tr class="GridHeader">
    </tr>
    <tr class="GridItem">
    </tr>
    <tr class="GridItem">
      <td class="GridButtonColumn">
        <a href="javascript:__doPostBack('dgTime$_ctl2$_ctl0','')">
          <img src="images/toolbar/b_edit.gif">
        </a>
      </td  

发送 Tim 以获得答案。现在我可以选择第一个子表单按钮来打开它。

links(j).click   'j = 0 

然后我保存、关闭并返回主窗体。但是当我尝试这样做时

links(j).click   'j = 2 this time

我第二次收到运行时错误 70:权限被拒绝。任何帮助将不胜感激。 问候

【问题讨论】:

  • 嗯,这是一个锚,而不是一个按钮。所以通过 tagName 选择它
  • 很抱歉,我不理解您的评论。如何在上面的 HTML 代码中找到 tagName?是 "dgTime$_ct12$_ct10" 吗?如果是这样,如果我事先不知道会有多少,我怎么能选择这个和后面的(ct12$、ct13$ 等等)?发送。
  • 嗯,这也不是我指的标签名称......developer.mozilla.org/en-US/docs/Web/API/…

标签: html excel vba internet-explorer web-scraping


【解决方案1】:

在蒂姆·威廉姆斯 (Tim Williams) 的帮助下,我终于找到了遗漏的最后一个细节。这是下面的最终代码。

Private Sub Open_multiple_sub_pages_from_main_page()


Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim valeur_heure As Object


' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True

' Send the form data To URL As POST binary request
IE.navigate "http://webpage.com/"

' Wait while IE loading...
While IE.Busy
        DoEvents
Wend


Set objCollection = IE.Document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
    If objCollection(i).Name = "txtUserName" Then
        ' Set text for search
        objCollection(i).Value = "1234"
    End If
    If objCollection(i).Name = "txtPwd" Then
        ' Set text for search
        objCollection(i).Value = "password"
    End If

    If objCollection(i).Type = "submit" And objCollection(i).Name = "btnSubmit" Then ' submit button if found and set
        Set objElement = objCollection(i)
    End If
    i = i + 1
Wend
objElement.Click    ' click button to load page

' Wait while IE re-loading...
While IE.Busy
        DoEvents
Wend

' Show IE
IE.Visible = True
Set Doc = IE.Document

Dim links, link

Dim j As Integer                                                                    'variable to count items
j = 0
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
While j <= n                                    'loop to go thru all "a" item so it loads next page
    links(j).Click
    While IE.Busy
        DoEvents
    Wend
    '-------------Do stuff here:  copy field value and paste in excel sheet.  Will post another question for this------------------------
    IE.Document.getElementById("DetailToolbar1_lnkBtnSave").Click              'save
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)                                   'wait
    Loop
    IE.Document.getElementById("DetailToolbar1_lnkBtnCancel").Click            'close
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)                                   'wait
    Loop
    Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
    j = j + 2
Wend    
End Sub

【讨论】:

    【解决方案2】:
    IE.Document.getElementById("dgTime").getElementsByTagName("a")(0).Click
    

    编辑:遍历集合(项目的出现顺序应与它们在源文档中的顺序相同)

    Dim links, link 
    
    Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
    
    'For Each loop
    For Each link in links
        link.Click
    Next link
    
    'For Next loop
    Dim n, i
    n = links.length
    For i = 0 to n-1 Step 2
        links(i).click
    Next I
    

    【讨论】:

    • Tx Tim 的答案。这将使我能够单击第一个元素。所以我正在考虑使用以下内容打开所有项目。设置 buttonCollection = IE.Document.getelementbyid"dgTime").getElementsByTagName("a") '(0) j = 1 'buttonCollection.Length While j
    • Tx 再次提姆,我想我可以提一下我需要选择所有其他元素,第一、第三、第五等等。如果我使用“for each”方法,如何跳过一个?我的最后一个问题不够精确,我很抱歉。感谢您的慷慨时间和知识。
    • 我一直在寻找使用“link.click”方法的解决方案。所以为了确保我不会再为同样的事情烦恼,我怎么能选择让我们说上面答案中“链接”中的“链接”的第 9 项
    【解决方案3】:

    CSS 选择器:

    使用 img[src='images/toolbar/b_edit.gif'] 的 CSS 选择器

    这表示选择带有img标签的元素,其属性src的值为'images/toolbar/b_edit.gif'


    CSS 查询:


    VBA:

    您可以使用document 的.querySelector 方法应用选择器。

    IE.document.querySelector("img[src='images/toolbar/b_edit.gif']").Click
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      相关资源
      最近更新 更多