-
June 2nd, 2017, 04:40 PM
#1
[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.
-
June 2nd, 2017, 10:32 PM
#2
Re: Automate Internet Explorer
Can you enumerate the elements get their class names?
-
June 3rd, 2017, 03:02 AM
#3
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.
-
June 3rd, 2017, 03:04 AM
#4
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)
-
June 3rd, 2017, 08:24 AM
#5
Re: Automate Internet Explorer
Originally Posted by Genotypek
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.
-
June 3rd, 2017, 08:30 AM
#6
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.
-
June 3rd, 2017, 09:18 AM
#7
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.
-
June 3rd, 2017, 09:54 AM
#8
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.
-
June 3rd, 2017, 02:58 PM
#9
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".
-
June 3rd, 2017, 03:27 PM
#10
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)
-
June 3rd, 2017, 05:18 PM
#11
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. :/
-
June 4th, 2017, 02:50 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|