[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.
Re: Automate Internet Explorer
Can you enumerate the elements get their class names?
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.
Re: Automate Internet Explorer
[Thread moved as c++/cli]
Re: Automate Internet Explorer
Quote:
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.
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).
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.
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?
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".
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!
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. :/
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. :)