Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

Close Button, getElementById, overlay

I have a screen scraper app.   I'm scraping a page that has an overlay - it's that object that makes it look a modal popup, but actually is not.  Now, there are elements within the overlay that I extract data from.  These elements all have ID values.  Now, the overlay has a "Close" button.  I want to click the close button.   The button does not have an ID, so I'm attempting to find it my looping through all the DOM elements and picking off the buttons, by class name, and then examining the text on the button.   However, my loop through all the DOM elements never finds this button that is located within the overlay.   There are other buttons on the page that are not within the overlay, and I do find these within the loop.

Any thoughts on why I cannot find the button within the overlay?
Avatar of Roopesh Reddy
Roopesh Reddy
Flag of India image

Hi,

I think you can try with jQuery CSS selector!

<input type="button" id="btn" value="Click" /> 

    <script type="text/javascript">
        $(document).ready(function () {
           var btnText = $('#btn').attr('value');
           alert(btnText);
        });
    </script>

Open in new window


Hope it helps u...
Avatar of HLRosenberger

ASKER

To use JQuery, don't I need to own the page?  The JQuery, like Javascript, runs in the page on the client?

I have a .NET console app that extracting data out of the DOM of someone elses page.
Avatar of Obadiah Christopher
Did u try and find the button when the overlay is not opened?

What method are you using to loop through the buttons?

Is it possible that you are not able to find the button since it is inside another div and not the outermost div?
First, you need to make sure the button exists on the page you're scraping like informaniac suggests.

Are you recursively looping through the elements? You need to make sure you find any elements within elements within elements. Share how you're doing the loop. We might be able to help with a recursive mechanism for getting them all. Something like:

       
 public static Control _prvGetFirstControlTypeFromControlCollection( Type param_Control_Type, Control param_Control )
        {
            Control foundControl = null;
            ControlCollection ccc = param_Control.Controls;
            foreach ( Control c in ccc )
            {
                if ( c.GetType() == param_Control_Type )
                {
                    foundControl = c;
                    break;
                }
                else
                {
                    if ( c.Controls.Count > 0 )
                    {
                        foundControl = _prvGetFirstControlTypeFromControlCollection( param_Control_Type, c );
                    }
                }
            }
            return foundControl;
        }

Open in new window


...but this method that I wrote only gets the first instance of a control of the type specified. Plus, it is used by an ASP .NET page that is generating a web page, not a screen scraper. I added it to show you how you might go about implementing your own though.
informaniac

It is inside another div. The overlay is a div.   So, how then do I find it?  I'm doing this to find the button:

 Public Function FindCloseButton(ByVal doc As Object) As HTMLButtonElement

        Dim btn As HTMLButtonElement

        FindCloseButton = Nothing
        For i As Integer = 0 To doc.all.Length - 1

            ' Find Input Element
            If TypeName(doc.all(i)) = "HTMLButtonElementClass" Then

                btn = doc.all(i)
                If btn.innerText = "Close" Then
                    FindCloseButton = btn
                    Exit For
                End If

            End If
        Next i

    End Function
Hi,

Did you tried the solution which i have provided?

Moreover, are you looking for solution in (Server Side)Code behind or Client Side?
Server side.  There is no "client side" in effect, as this is a console app that is a screen scraper.  I' using the SHDocVw.InternetExplorer object.
ASKER CERTIFIED SOLUTION
Avatar of HLRosenberger
HLRosenberger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the only solution that worked for me.