+ Reply to Thread
Results 1 to 1 of 1

Manipulating Combobox in Internet Explorer

Hybrid View

mc84excel Manipulating Combobox in... 12-15-2019, 09:38 PM
  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Manipulating Combobox in Internet Explorer

    I have been automating the IE browser to conduct web scraping for many years across a number of sites.

    I have recently come across a combobox that will only work properly after it has been manipulated by a human.

    To clarify - if I automate the selection of the dropdown using code, the combobox value changes correctly. Or rather - it APPEARS to. But then the automation clicks the download button and the file that downloads is not the option that was selected in the combobox!

    But if I pause the code at this point and I change the dropdown manually in the browser before letting the code continue, the download is the option that was selected.

    My question is this:

    How do I alter my combobox code so that the browser actions on the combobox are the same as what would have been sent if the combobox had had its dropdown changed manually?

    (Or to put it another way - how can the function be written so that its automation will mimic a humans input on a combobox)



    Relevant extract below:
    Option Explicit
    
    'Requirements:
    '   Reference Library =     Microsoft Internet Controls
    '   DLL =                   SHDocVw (dll/COM Shell Document Viewer)
    
    #If VBA7 And Win64 Then
        ' 64 bit Excel
        Private Declare PtrSafe Sub Sleep _
                    Lib "Kernel32" ( _
                    ByVal dwMilliseconds As LongLong)
    
    #Else
        ' 32 bit Excel
        Private Declare Sub Sleep _
                    Lib "Kernel32" ( _
                    ByVal dwMilliseconds As Long)
    
    #End If
    
    Private mappIE                  As InternetExplorerMedium
    Private mblnSilentFails         As Boolean
    
    Public Function fnblnActionInputComboBox_ByITextByID(ByVal strElmtId As String, ByVal strOptionInnerText As String) As Boolean
        Dim objElement As Object
        Dim blnResult As Boolean
        Dim i As Long
    
        Set objElement = pfnobjSetElementById(strElmtId, mblnSilentFails)
        If objElement Is Nothing Then
            GoTo ExitProcedure
        End If
    
        With objElement
            If .Type <> "select-one" Then
                'Call pDebugPrintElementNotFound("Type", "select-one (but matched on Id:=" & strElmtId & ")")
                MsgBox "Element Id '" & strElmtId & " has Type '" & .Type & "' (Code was expecting 'select-one')", vbCritical
                GoTo ExitProcedure
            End If
    
            If LenB(strOptionInnerText) Then
                .Focus
    
                For i = 0 To .Options.Length - 1
    
                    If InStr(1, .Options(i).innerText, strOptionInnerText, vbTextCompare) Then
    
                        strOptionInnerText = .Options(i).innerText
    
                        .Options(i).Focus
                        Sleep 20
                        .Options(i).Selected = True
                        Sleep 20
                        .Options(i).Click
                        Sleep 20
                        .Options(i).fireevent ("onchange")
    
                        .selectedIndex = i
    
                        Do Until .ReadyState = "complete"
                            Sleep 100
                        Loop
    
                        blnResult = True
                        Exit For
                    End If
                Next i
            End If
        End With
    
    ExitProcedure:
        Set objElement = Nothing
        fnblnActionInputComboBox_ByITextByID = blnResult
    End Function
    
    Private Function pfnobjSetElementById(ByVal strElmtId As String, ByVal blnBatchMode As Boolean) As Object
    '/ this function is to deal with a known bug in IEs use of GetElementById
        Dim objElement As Object
        Dim strMsg As String
        Dim blnSuccess As Boolean
    
        On Error Resume Next
        Set objElement = mappIE.Document.GetElementById(strElmtId)
        On Error GoTo 0
    
        If objElement Is Nothing Then
            'If Not blnBatchMode Then
                'Call pDebugPrintElementNotFound("Id", strElmtId)
            'End If
        ElseIf objElement.ID = strElmtId Then
            blnSuccess = True
            Set pfnobjSetElementById = objElement
        Else
            With objElement
                strMsg = "Known IE bug found. Element '" & .ID & "' only matches the required Id ('" & strElmtId & "') on its Value! ('" & .Value & "')"
            End With
        End If
    
        If LenB(strMsg) Then
            If blnBatchMode Then
                Debug.Print strMsg
            Else
                MsgBox strMsg
            End If
        End If
    
    ExitProcedure:
    
        ' scrap object
        If Not TypeName(objElement) = "Nothing" Then
            Set objElement = Nothing
        End If
    End Function

    I am unable to provide the website or the HTML code. In any case, I want this function to be generic enough so that I can apply it to any html combobox and it will work.
    Last edited by mc84excel; 12-16-2019 at 09:15 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. web scraping using internet explorer (select combobox)
    By daboho in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-27-2019, 07:55 AM
  2. Help With internet explorer
    By bryantbergman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2016, 04:09 PM
  3. [SOLVED] How to toggle focus between INTERNET explorer window and WINDOWS explorer window using VBA
    By tnuis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2013, 03:29 PM
  4. How to use tab down on internet explorer?
    By allepavankumar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2012, 10:28 AM
  5. VBA with Internet Explorer
    By mike20255 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-29-2010, 01:30 PM
  6. Using internet explorer through VBA
    By bodis in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-04-2010, 05:47 AM
  7. internet explorer
    By doris in forum Excel General
    Replies: 1
    Last Post: 01-05-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1