CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2017
    Posts
    23

    [RESOLVED] Automate Internet Explorer

    Hello.
    I'm trying to automate internet explorer, but i stucked on problem, that an element doesn't have any id or name, only class.
    For input I can easu use:
    Code:
    hIE->Document->GetElementById( "user" )->InnerText = "genotypek";
    There's no function like GetElementsByClassName.
    I can't click the login button, which has only class named "btn-login".

    Any help?
    Greetings, genotypek.
    Last edited by Genotypek; June 2nd, 2017 at 04:42 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Automate Internet Explorer

    Can you enumerate the elements get their class names?

  3. #3
    Join Date
    Apr 2017
    Posts
    23

    Re: Automate Internet Explorer

    Only one button has this class, no more. It's full HTML is:
    Code:
    <a class="btn-login" href="#">Logowanie</a>
    I tried to do it like this:
    Code:
    	System::Windows::Forms::HtmlElementCollection^ Elements = GUI_Przegladarka->Document->GetElementsByTagName("a");
    	for ( int i = 0; i < sizeof( Elements ); i++ )
    		if ( Elements[i]->GetAttribute( "Class" )->ToString() == "btn-login" ) Elements[i]->InvokeMember( "Click" );
    But it doesn't work.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Automate Internet Explorer

    [Thread moved as c++/cli]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Automate Internet Explorer

    Quote Originally Posted by Genotypek View Post
    Only one button has this class, no more. It's full HTML is:
    Code:
    <a class="btn-login" href="#">Logowanie</a>
    I tried to do it like this:
    Code:
    	System::Windows::Forms::HtmlElementCollection^ Elements = GUI_Przegladarka->Document->GetElementsByTagName("a");
    	for ( int i = 0; i < sizeof( Elements ); i++ )
    		if ( Elements[i]->GetAttribute( "Class" )->ToString() == "btn-login" ) Elements[i]->InvokeMember( "Click" );
    But it doesn't work.
    Just get all the elements and walk that list (i.e., don't filter by GetElementsByTagName). I say this to do it as a test to see if you can find the button element at all. If you can find it, then you can look at ways to not have to walk the entire dom.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Automate Internet Explorer

    Not sure if that will already make it work, but your for loop limit should be Elements.Count instead of sizeof(Elements).
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Apr 2017
    Posts
    23

    Re: Automate Internet Explorer

    Yes, I can find the button, it's index is 62 I can even click it. But:
    Elements[62]->GetAttribute("class"); is null
    Elements[62]->InnerText; is "Logowanie"
    Elements[62]->InnerHtml; is "Logowanie" too.

    There's no information about class. Elements 0-20 has classes, hrefs etc.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Automate Internet Explorer

    Well, I recall from one of my own projects, that, at least sometimes, the class attribute may not be accessible via GetAttribute(), and I don't know the reason either. Can't you use any other property, like InnerText to identify the button?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Apr 2017
    Posts
    23

    Re: Automate Internet Explorer

    Yes, using InnerText isn't problem there, but the problem is, that there's <h1> with Innertext "Logowanie", and <a> (this button) with InnerText "Logowanie".
    Both InnerHtml code is the same as InnerText. No Attributes, only word "Logowanie".

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Automate Internet Explorer

    I'm not a c++/cli person, but looking at this you have
    62 - Innertext Logowanie, no class
    h1 - Innertext Logowanie has a class
    a - Innertext Logowanie has a class

    Can't you do something like this - enumerate for all Innertext == Logowanie. Then for each one check if GetAttribute("class") is null. if it is then its the one you want. If it's not then continue. If you check each found one and all have a class then you're got a problem!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Apr 2017
    Posts
    23

    Re: Automate Internet Explorer

    But class is null for both of them.
    One of them is <a> has id="login" - it's common label.
    Second one is <a> has class="btn-login", but this class can't be accesses via GetAttribute("class")
    I can't do it using InnerText too, because in another website language it won't be the same. :/

  12. #12
    Join Date
    Apr 2017
    Posts
    23

    Re: Automate Internet Explorer

    Okay I have resolved this problem.
    InnerHtml and InnerText didn't work, but I found outerHtml property, so simply:

    Code:
    System::Windows::Forms::HtmlElementCollection^ Elements = GUI_Przegladarka->Document->GetElementsByTagName("a");
    
    	for ( int i = 0; i < Elements->Count; i++ )
    	{
    		if ( SystemStringToStdStringR( Elements[i]->OuterHtml ).find( "class=\"btn-login\"" ) != std::string::npos )
    		{
    			Elements[i]->InvokeMember( "Click" );
    		}
    	}
    Works fine.
    Thank you for help, I will need it more, but this in another thread.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured