none
Loop to get html elements exiting before criteria met RRS feed

  • Question

  • I am trying to create a HTML interface using Excel-VBA. 

    The following piece of code is supposed to get a collection of HTML elements from a JavaScript popup. For some reason the Do statement is often exited even though the collection contains no elements. I've tried using a recursive function, other loop/for statements and using DoEvents to correct this but nothing has worked.  

    Set col = Nothing   
    Do        
        Set col = ie.document.getElementById(data.pop).getElementsByTagName(tag)    
    Loop While col.Length < 1

    If I add msgbox col.length directly after this I receive a message with the number of elements there should have been in the collection even though the collection is empty. I have also had similar issues with the isObject function and cannot understand why. 

    Would anybody be able to shed some light on this? 
        

    Friday, June 10, 2016 2:43 AM

All replies

  • What is the data type of col? Also, is it possible for us to access the pop-up link (can you share it)?

    http://www.ambienteoffice.com.br - http://www.clarian.com.br

    Friday, June 10, 2016 10:30 AM
  • Either post the website or save the webpage (cntl+s) to disk and post the webpage to a file sharing site and be sure to post the files directory.

    I've had a lot of problems with IE 11.  I would recommend using Selenium Basic rather than Microsoft Internet Controls.  It comes with a lot of Excel examples.  It supports selecting elements with Xpath which is extremely powerful.  It supports Firefox, Chrome, IE and Edge.  I just finished a project with it and was really impressed.  It seems to be well supported with new updates every few months.  The latest release is 2.0.9.0.

    Selenium is used to test websites and is used by almost everyone.  You can use it with C#, Java, Ruby and other languages.  Someone developed Selenium Basic which supports VB.NET and VBA.

    Friday, June 10, 2016 2:11 PM
  • Thanks for the help. I'll give Selenium a go and see if that changes anything

    col is declared as an Object. The website is a company one and requires login details to access.
    I've saved the webpage, is there an file sharing site you would recommend?  I've attached the HTML code for the popup below.

    <div id="popup_0" data-role="popup" class="jqm4gwt-popup ui-popup ui-overlay-shadow ui-corner-all ui-body-c" data-history="false" data-overlay-theme="a">
        <div data-role="header" class="jpm4gwt-header ui-header ui-bar-a" role="banner">
            <h1 class="ui-title" role="heading" aria-level="1">Sites</h1>
        </div>
        <div id="gwt-uid-260" style="overflow-x: hidden; overflow-y: auto; height: 389px; width: 737px;">
            <form class="ui-listview-filter ui-bar-c" role="search">
                <div class="ui-input-search ui-shadow-inset ui-btn-corner-all ui-btn-shadow ui-icon-searchfield ui-body-c">
                    <input placeholder="Filter items..." data-type="search" class="ui-input-text ui-body-c">
                    <a href="https://iamwind.energy.siemens.com/mcWindGhr/#" class="ui-input-clear ui-btn ui-btn-up-c ui-shadow ui-btn-corner-all ui-fullsize ui-btn-icon-notext ui-input-clear-hidden" title="clear text" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="delete" data-iconpos="notext" data-theme="c" data-mini="false">
    					<span class="ui-btn-inner">
    						<span class="ui-btn-text">clear text</span>
    						<span class="ui-icon ui-icon-delete ui-icon-shadow">&nbsp;</span>
    					</span>
    				</a>
    			</div>
    		</form>
    		<ul class="jqm4gwt-list ui-listview" data-role="listview" id="gwt-uid-252" data-filter="true">
    			<li class="jqm4gwt-listitem ui-btn ui-btn-up-c ui-btn-icon-right ui-li-has-arrow ui-li ui-first-child" id="gwt-uid-253" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">DP2 Vessel Island Crown</h3>
    							<p class="ui-li-desc"></p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    			<li class="jqm4gwt-listitem ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" id="gwt-uid-254" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">DP2 Vessel Island Crown (DK)</h3>
    							<p class="ui-li-desc">When working/staying on the vessel, which is lying in the harbour in Denmark</p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    			<li class="jqm4gwt-listitem ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-hover-c" id="gwt-uid-255" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">Emden harbour</h3>
    							<p class="ui-li-desc"></p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    			<li class="jqm4gwt-listitem ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" id="gwt-uid-256" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">Hotel Vessel Regina Baltica</h3>
    							<p class="ui-li-desc"></p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    			<li class="jqm4gwt-listitem ui-btn ui-btn-up-c ui-btn-icon-right ui-li-has-arrow ui-li" id="gwt-uid-257" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">Hotel Vessel Regina Baltica (DK)</h3>
    							<p class="ui-li-desc">When working/staying on the vessel, which is lying in the harbour in Denmark</p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    			<li class="jqm4gwt-listitem ui-btn ui-btn-up-c ui-btn-icon-right ui-li-has-arrow ui-li" id="gwt-uid-258" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">Installation Vessel Sea Challenger</h3>
    							<p class="ui-li-desc"></p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    			<li class="jqm4gwt-listitem ui-btn ui-btn-up-c ui-btn-icon-right ui-li-has-arrow ui-li ui-last-child" id="gwt-uid-259" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c">
    				<div class="ui-btn-inner ui-li">
    					<div class="ui-btn-text">
    						<a href="https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion" class="ui-link-inherit">
    							<h3 class="ui-li-heading">Installation Vessel Sea Challenger (DK)</h3>
    							<p class="ui-li-desc">When working/staying on the vessel, which is lying in the harbour in Denmark</p>
    						</a>
    					</div>
    					<span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>
    				</div>
    			</li>
    		</ul>
    	</div>
    	<div data-role="footer" class="jpm4gwt-footer ui-footer ui-bar-a" role="contentinfo">
    		<div class="gwt-Label ui-navbar ui-mini" data-role="navbar" id="gwt-uid-261" role="navigation">
    			<ul class="ui-grid-a">
    				<li class="ui-block-a">
    					<a class="jqm4gwt-button ui-btn ui-btn-inline ui-btn-up-a" href="javascript:;" data-role="button" id="gwt-uid-263" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-theme="a" data-inline="true">
    						<span class="ui-btn-inner">
    							<span class="ui-btn-text">Cancel</span>
    						</span>
    					</a>
    				</li>
    				<li class="ui-block-b">
    					<a class="jqm4gwt-button ui-btn ui-btn-up-a ui-btn-inline" href="javascript:;" data-role="button" id="gwt-uid-265" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-theme="a" data-inline="true">
    						<span class="ui-btn-inner">
    							<span class="ui-btn-text">Delete</span>
    						</span>
    					</a>
    				</li>
    			</ul>
    		</div>
    	</div>
    </div>>

    Friday, June 10, 2016 3:31 PM
  • You need to save not only the webpage but everything associated with webpage.  Do a cntl+s and save it to an empty directory. If you use IE make sure you select html and complete website.  You will see the webpage and a directory.  Logout from website.  You should be able to double click webpage and open it in browser.  Do you see page with popup?  Depends on Javascript it may not show?  If it does, post it to a file sharing site like OneDrive or Dropbox.  Make sure it is public viewable.

    • Edited by mogulman52 Friday, June 10, 2016 7:57 PM
    Friday, June 10, 2016 7:51 PM
  • I've saved the webpage. But when I open it a loading bar shows briefly in the main window and then the page goes blank.

    Playing around the with the site I've have found there are almost identical popups that can be accessed without having to login. If you go to: https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion 

    Put in login details for Name, ID password, retype (password must be upper/lower case number and symbols). Then click 'back' followed by 'menu' and an almost identical popup box should be visible titled 'Commands'.
    Saturday, June 11, 2016 2:30 PM
  • Using SeleniumBasic I was able to extract the list. I got this:

    Logon
    Add user
    Change password
    Sync password
    Delete user

    Dim drv As Selenium.ChromeDriver
    Dim text As String
    Dim ele As Selenium.WebElement
    
    Sub Login()
       '' Add reference to selenium type library
      Dim xPath As String
      Dim browser As String
      
      Set drv = New Selenium.ChromeDriver
      drv.Get "https://iamwind.energy.siemens.com/mcWindGhr/#mCompanion"
      xPath = "//input[@id='gwt-uid-47']"  ' Name
      If GetElement(xPath) Then
        ele.SendKeys "Fake Name"
        xPath = "//input[@id='gwt-uid-50']"   'ID
        If GetElement(xPath) Then
          ele.SendKeys "MyFakeID"         '
          xPath = "//input[@id='gwt-uid-56']"   'Password
          If GetElement(xPath) Then
            ele.SendKeys "MyFakePass12$"
            xPath = "//input[@id='gwt-uid-59']"   'Retype
            If GetElement(xPath) Then
              ele.SendKeys "MyFakePass12$"         '
              xPath = "//a[@id='gwt-uid-42']"    ' Back
              If GetElementClick(xPath) Then
                xPath = "//a[@id='gwt-uid-21']"    ' Menu
                If GetElementClick(xPath) Then
                  For Each ele In drv.FindElementsByXPath("//h3[@class='ui-li-heading']")
                    Debug.Print ele.text
                  Next ele
                End If
              End If
            End If
          End If
        End If
      End If
    End Sub
    
    Function GetElement(xPath As String) As Boolean
      On Error GoTo Handler
      
      text = ""
      Set ele = drv.FindElementByXPath(xPath)
      text = ele.text
      GetElement = True
      Exit Function
    Handler:
      Err.Clear
      GetElement = False
    End Function
    
    'Function GetElements(xPath As String) As Boolean
    '  On Error GoTo Handler
    '
    '  Set eles = drv.FindElementsByXPath(xPath)
    '  GetElements = True
    '  Exit Function
    'Handler:
    '  Err.Clear
    '  GetElements = False
    'End Function
    
    
    Function GetElementClick(xPath As String) As Boolean
      On Error GoTo Handler
      
      text = ""
      Set ele = drv.FindElementByXPath(xPath)
      ele.Click
      drv.Wait (1000)
      GetElementClick = True
      Exit Function
    Handler:
      Err.Clear
      GetElementClick = False
    End Function
    
    

    Saturday, June 11, 2016 8:37 PM
  • Thanks for your assistance, unfortunately on trying to implement Selenium I found I had issues with both IE and Chrome due to security restrictions on my laptop.  

    I've since managed to find a way around without using Selenium. I'm not certain what the root cause was but if I call the code from a method with col as a local variable (instead opposed to global which I had been using previously), then the code works ok.
    Wednesday, June 15, 2016 7:40 PM
  • By default Selenium creates a brand new profile every time it runs.  You can force it to use a specific profile.  I do this so I can have certain addins every time.

        drv.SetProfile "Selenium", persistant:=True

    Wednesday, June 15, 2016 10:16 PM