1

So I got a web page that has the input tag

<div id="mainID" class="outside">
   <div class="inside">
      <input id="5443" name="43" type="checkbox" class="classname" /> 
      <input id="5444" name="44" type="checkbox" class="classname" /> 
      <input id="5445" name="45" type="checkbox" class="classname" /> 
      <input id="5446" name="46" type="checkbox" class="classname" /> 
      <input id="5447" name="47" type="checkbox" class="classname" /> 
   </div>
</div>

The ID and name is varied, but the class name is always the same - so using the class tag (classname), how can I retrieve the ID name, 5443 (well all of them one by one in a for loop) using the visual basic browser? I dont want the name tag information.

EDIT: added code

QHarr
  • 83,427
  • 12
  • 54
  • 101
Pengiuns
  • 131
  • 2
  • 9

3 Answers3

1

CSS selector:

If you are using VBA then once you have got your HTML inside of an HTMLDocument e.g.

IE.document, then you can use a querySelectorAll method of document to apply a CSS selector to get the elements of interest.

The CSS selector to apply is: #mainID div.inside input

This says get elements where there is an input tag inside of a div with className inside, inside of id mainID. The "#" is id, and the "." is className. The " " represents nesting.


CSS query:

The selector on your HTML returns the following elements

Elements


VBA:

You can use the following to retrieve the elements and then parse the HTML using Split to get the Ids within. querySelectorAll returns a nodeList which you loop the length of. You cannot use a For Each Loop. It will crash Excel!

Option Explicit
Public Sub GetInfo()
    Dim aNodeList As Object, i As Long
    'Other code to get the HTMLDocument.......
    Set aNodeList = HTMLDocument.querySelectorAll("#mainID div.inside  input")

    For i = 0 To aNodeList.Length - 1
        Debug.Print GetId(aNodeList(i).innerHTML)
        'Debug.Print GetId(aNodeList.item(i).innerHTML) '<== Sometimes the syntax is like this rather than the above
    Next i
End Sub

Public Function GetId()
    GetId = Replace$(Split(Split(s, "id=")(1), Chr$(32))(0), Chr$(34), vbNullString)
End Function
QHarr
  • 83,427
  • 12
  • 54
  • 101
0

I figured it out after messin around with the original post that I found on stackoverflow.com

Dim theElementCollection As HtmlElementCollection = Nothing
        theElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
        For Each curElement As HtmlElement In theElementCollection
            'If curElement.GetAttribute("classname").ToString = "example"  It doesn't work.  
            ' This should be the work around.
            If InStr(curElement.GetAttribute("classname").ToString, "yourcalssname") Then
                ' Doesn't even fire.
                ' InvokeMember(test) after class is found.
                MessageBox.Show(curElement.GetAttribute("InnerText"))

            End If
        Next
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
  • Didn't work sadly. I knew the jest of it and tried messing around with something similar to that before but didn't work. I forgot to add the code, which I added now. – Pengiuns May 23 '14 at 17:16
-2

Its not easy task at back-end. But client side its easy by jquery.

Here is the link as your answer at c#.

How to select an element by Class instead of ID in ASP.NET?

"You cannot do it based on just the class name. A class name and id are totally unrelated. Just think about it: the same class can be used for different objects, but id values should always be unique on the page. So, the request simply makes no sense; the answer it obvious." refer link.

http://www.codeproject.com/Questions/720610/csharp-code-for-get-ID-of-the-control-using-its-cl

C# how to get the name (in string) of a class property?

This below example for clint side via jquery. Its quite easy.

At browser side use jquery or javascript. In your case use jquery.

Add latest jquery in your HTML page and write the code in script tag as

<script>
  $( document ).ready(function() {        
        $('.test').click(function() {
               alert( this.id );
        });
    });
</script>

Refer links : - jquery: get id from class selector

you can do search from variety condition

Get the ID by Class name JQuery

Community
  • 1
  • 1
Ajay2707
  • 5,690
  • 6
  • 40
  • 58