2
votes

I am trying to check a checkbox on a web page using Excel VBA. My code did not work. (There was no error but the tick was ticked)

How can I make the tick show when I do it on the web page?

Am I doing something wrong with the web scraping?


HTML:

<div class="slick-header ui-state-default">
  <div class="slick-header-columns ui-sortable" style="left: -1000px;" unselectable="on">
    <div title="Sélectionner / désélectionner tout" class="ui-state-default slick-header-column" id="slickgrid_951473vssGrid_rowSelect_123527" style="width: 21px;">
      <div class="header">
        <input type="checkbox">
      </div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473purchase_order" style="width: 104px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Bon de commande</span></div><span class="slick-sort-indicator"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473purchase_order_date" style="width: 104px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Date de la commande</span></div><span class="slick-sort-indicator slick-sort-indicator-asc"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473latest_ship_date" style="width: 103px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Date d'expédition maximale</span></div><span class="slick-sort-indicator"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473vendor_code" style="width: 103px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Vendor code</span></div><span class="slick-sort-indicator"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473payee_code" style="width: 103px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Code bénéficiaire</span></div><span class="slick-sort-indicator"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473warehouse_id" style="width: 103px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Entrepôt de destination</span></div><span class="slick-sort-indicator"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473currency_code" style="width: 103px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Devise</span></div><span class="slick-sort-indicator"></span>
      <div class="slick-resizable-handle"></div>
    </div>
    <div class="ui-state-default slick-header-column" id="slickgrid_951473status" style="width: 103px;">
      <div class="header slick-sort-header"><span class="slick-column-name">Statut</span></div><span class="slick-sort-indicator"></span></div>
  </div>
</div>

VBA code :

Sub automationAmazon()
    Dim ie As Object
    Set ie = CreateObject("internetexplorer.application")
    With ie
        .Visible = True
        .navigate "link"

        Do While .busy
            DoEvents
        Loop

        Do While .readyState <> 4
            DoEvents
        Loop

    End With

    Set Email = ie.document.getelementbyid("ap_email")
    Email.Value = "[email protected]"

    Set Password = ie.document.getelementbyid("ap_password")
    Password.Value = "xxx"

    Set submit = ie.document.getelementbyid("signInSubmit")
    submit.Click

    With ie
        .navigate "link"
        Do While .busy
            DoEvents
        Loop

        Do While .readyState <> 4
            DoEvents
        Loop

    End With

    ie.document.all("searchBy").Value = "PURCHASE_ORDER"
    ie.document.all("purchaseOrderNumber").Value = ThisWorkbook.Sheets("Data").Cells(1, 2).Text
    ie.document.getelementbyid("create-invoice-search-submit").Click

    Dim cBox As Object
    Set cBox = ie.document.getElementsByClassName("header")(0)
    cBox.Click
End Sub

The HTML:

HTML


1
I am sorry biut it is a link for an intranet.I can share with you source page if this helpsilyes
I tried to put all the info :-)ilyes

1 Answers

1
votes

The element you are after is inside an iframe which is inside a form. You have to negotiate them. I have read your html in from a file.

The bit you need is:

ie.document.getElementsByTagName("iframe")(0).document.getElementsByTagName("input")(4).Click

Form and iframe:

image


My code:

Option Explicit
Public Sub test()
    Dim oFSO As Object, oFS As Object, sText As String
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFS = oFSO.OpenTextFile("C:\Users\User\Desktop\doc.html")

    Do Until oFS.AtEndOfStream
        sText = oFS.ReadAll()
    Loop

    Dim html As New HTMLDocument, b As Object
    html.body.innerHTML = sText
    Set b = html.getElementsByTagName("iframe")(0).document.getElementsByTagName("input")(4)
    Debug.Print b.outerHTML
    b.Click
End Sub

Verifying correct element:

The printout to the immediate window of the selected element's outerHTML shows that the correct element is selected:

output


Ensuring pages load each after each click

Use

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

after each click event to allow the page to load. That includes after the clicks you do in the login sequence before attempting this click.


Reading in HTML file script adapted from ozknows.