Jump to content

Write a simple conditional script to act on html/javascript buttons


Koopa
 Share

Recommended Posts

Hi Everybody,

Im looking for something which could perform actions on a defined web page which never changes, in order to prevent me from wasting my time on this task everyday.

After a long hunt for this treasure, I found AutoIt. However, im not really used to programmation, therefore I come here to request your help.

At the beginning I wanted to use a program which replicates your mouse/keyboard moves in the windows GUI, but since I need conditions, I abandonned this idea.

Here is the really simple task I have to do :

The good page is openened, and I launch the scriipt :

- It goes to new entry button on the web page and click it.

- Then, it selects the entry A in the drop-down menu

- Then it clicks on record

- Then it clicks on confirm on the next page

Here comes the condition :

Normally, since its confirmed, the website returns to the original page (the one with the new entry button).

- But, if the entry is already recorded, it will first display a page with the message Entry already recorded with a OK button that one has to click to go back to the original page.

Then the script executes himself again till it has entered all the entries in the drop down menu.

With that, I would already be so happy.

(The best thing would be to identificate each entry in the drop down menu by its name, cause an entry which is already recorded would still appear in the menu after being tested.)

Could you help me to write such a script ?

Thanks a lot for your help,

Koopa.

Link to comment
Share on other sites

Thanks for your answers.

I cant find the help file you re speaking about. I have just browsed through the online documentation. I have also searched the forum, but the problem is that all of the discussion about IE are very specific, and since i have only programmed basicely in Turbo Pascal, i almost knows nothing...

So starting from scratch is almost impossible. Reason of my help request :)

@jefhal : the page is private. It request a login and is located on an intranet. But i have the sources of each page, and therefore, i guess, the name of the button.

Regarding ie.au3, as i put it, i searched for it but had problems to understand :/

Link to comment
Share on other sites

I cant find the help file you re speaking about.

Should be at Start-->All Programs--> AutoIt v3--> AutoIt Help File

I have just browsed through the online documentation. I have also searched the forum, but the problem is that all of the discussion about IE are very specific, and since i have only programmed basicely in Turbo Pascal, i almost knows nothing...

So starting from scratch is almost impossible. Reason of my help request :)

Believe me, it's not impossible. The examples in the help file are short, clear and usually explained step by step. To get started, I guess you could take a look at these functions:

_IE_Example Display a new browser window pre-loaded with documents to be used inIE.au3 examples or your own testing

_IE_Introduction Display introductory information about IE.au3 in a new browser window

_IECreate Create an Internet Explorer Browser window.

This example helped me a lot, for example:

#include <IE.au3>
$oIE = _IECreate ("http://www.google.com")
$oForm = _IEFormGetObjByName ($oIE, "f")
$oQuery = _IEFormElementGetObjByName ($oForm, "q")
_IEFormElementSetValue ($oQuery, "AutoIt IE.au3")
_IEFormSubmit ($oForm)
Link to comment
Share on other sites

heya,

So, i tried :)

I succeed in opening a IE page at the adress i want.

I tried to include the function which looks for an object,

and then, after, to include the function that clicks on this object. But that doesnt work. I just have my page loaded correctly.

here is my code :

#include <IE.au3>

$oIE = _IECreate ("http://www.xxxxx")

_IELoadWait ($oIE) <= I guess this is to tell the program to wait for the page to be loaded ? anyway i thought so, so i added that here.

$oSubmit = _IEFormGetObjByName ($oIE, "newentry") <= im looking for the button i want to click on.

_IEAction ($oSubmit, "click") <= i click on it

_IELoadWait ($oIE) <= i wait for the page to be loaded.

Here s the source code of the button. This is a javascript object.

<td width=220 align=center><font color=#AAAA77 face=verdana,arial size=2><input type=submit class=button name=newentry value="New Entry"></td>

When this clicking action will work, ill have to get the name of each object in the drop down menu, then select one and click on the "ok" button (which shouldnt be a problem if i know how to do this first step i copied up above).

Thanks for your help !

K.

Link to comment
Share on other sites

The Tab key is a real life saver. I don't program in IE, but I recommend the Tab key after the page loads to go through controls. (And of course, turn down the SendKeyDelay before you do that!)

send ("{TAB 5}") ; Switch links/controls five times in the open browser window, Enter key acts like a mouseclick.

Edited by RobertKipling
Link to comment
Share on other sites

tab key is a good idea, but only for the next step. Cause on my first page, there could be more or less link before the new entry button.

Therefore, i cant use tab, i have to go directly to this button, identified by its name.

Link to comment
Share on other sites

_IEFormGetObjByName is used to get the name of the form, not the button inside a form. For that you need _IEFormElementGetObjByName -- take a look at the examples in the helpfile.

Actually in this case you could replace your _IEFormGetObjByName with _IEGetObjByName, keeping all wlse the same and it should work.

BTW, _IECreate calls _IELoadWait for you but there is no harm in calling it again. You do need it after _IEAction, click however.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Yeah ! That works with the getobjectbyname command !

So, im now at the second stage : try to find a way to pick up an entry in the drop down menu.

after that, ill now how to click on the confirm button :)

any info about a drop down menu ?

Link to comment
Share on other sites

Here is the code of the drop drown menu (still javascript )

<form action=Plan method=post><SELECT name=planid size=1><OPTION VALUE=145702>aaa</OPTION><OPTION VALUE=151720>bbb</OPTION><OPTION VALUE=149966>ccc </OPTION><OPTION VALUE=151662>ddd </SELECT>

i ll have a look at the help file regarding the form. i come back to edit my post if i find something interesting.

EDIT : I would say that i have to use :

$oForm = _IEFormGetCollection ($oIE, 0)

$oQuery = _IEFormElementGetCollection ($oForm, 1)

to get a list of the objects in my menu and identify them.

Am i right ?

Then, how to pick one up before clicking on "ok" ?

EDIT2 :

I found that :

_IEFormElementOptionselect ($oSelect, 0, 1, "byIndex")

Sleep(1000)

?

Edited by Koopa
Link to comment
Share on other sites

ok guyz, i have spent so much time, but now, i can select the entries in my drop down menu. :)

However i can only select the first choice :/

$oForm = _IEFormGetObjByName ($oIE, "menu")

_IEFormElementOptionselect ($oForm, 0, 1, "byIndex")

What do mean the 0,1 here? I thought it was the "rank" of the element in the menu, but apparently no. How can i do to select rank 2 for instance ?

Here is the next issue :

After validating different menus and buttons, i arrive at a page :

either they say that my entry is already made, therefore i have to click on a OK button, either they say a last confirm, and the page is different and i have to click on another button.

So, i have to do something like :

IF you find the button "confirm" click on it, either click "confirm"

But i absolutely dont know the syntax. Can someone help me with that ?

2nd question :

In my drop down menu, i would like to store the name of the selected element in order to avoid to select it again at the next cycle. Is it complicated to do ?

Edited by Koopa
Link to comment
Share on other sites

ok guyz, i have spent so much time, but now, i can select the entries in my drop down menu. ;)

However i can only select the first choice :/

$oForm = _IEFormGetObjByName ($oIE, "menu")

_IEFormElementOptionselect ($oForm, 0, 1, "byIndex")

What do mean the 0,1 here? I thought it was the "rank" of the element in the menu, but apparently no. How can i do to select rank 2 for instance ?

_IEFormElementOptionselect ( ByRef $o_object, $s_string [, $f_select = 1 [, $s_mode = "byValue" [, $f_fireEvent = 1]]] )

The 0:

$s_string = Value used to match element - treatment based on $s_mode

In your case, the mode is "ByIndex", so you put 0 there (Index 0 element). If your mode is "byText", you put the string you want to match there.

The 1:

$f_select [Optional]: specifies whether element should be selected or deselected

-1 = Return selected state

0 = Deselect the element

1 = (Default) Select the element

Here is the next issue :

After validating different menus and buttons, i arrive at a page :

either they say that my entry is already made, therefore i have to click on a OK button, either they say a last confirm, and the page is different and i have to click on another button.

If the "your last entry is already made" message is always the same, you could just read the source code of the page and see if StringRegExp() can find it. If it finds the message then click the "OK" button. Else, click on the other one.

IF you find the button "confirm" click on it, either click "confirm"

What? :)

2nd question :

In my drop down menu, i would like to store the name of the selected element in order to avoid to select it again at the next cycle. Is it complicated to do ?

Use "byText" mode for _IEFormElementOptionselect ()

Hope I didn't misunderstand something.

Link to comment
Share on other sites

hey Nahuel, hey others,

Sorry for late answer but we are apparently in totaly different timezones <_<.

Ok, then, here is my code and the explanations. Please, have a look at it !

CODE
#include <IE.au3>

$oIE = _IECreate ("http://www.xxxxxxxx")

$oForm = _IEGetObjByName ($oIE, "newentry")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

$oForm = _IEFormGetObjByName ($oIE, "planid")

_IEFormElementOptionselect ($oForm, 0, 1, "byIndex")

$oForm = _IEGetObjByName ($oIE, "newentry")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

$oForm = _IEGetObjByName ($oIE, "confirm")

If $oForm=0 Then

$oForm = _IEGetObjByName ($oIE, "action")

_IEAction ($oForm, "click")

Else

$oForm = _IEGetObjByName ($oIE, "confirm")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

Endif

For this part :

CODE

$oForm = _IEGetObjByName ($oIE, "confirm")

If $oForm=0 Then

$oForm = _IEGetObjByName ($oIE, "action")

_IEAction ($oForm, "click")

Comments : the program looks for a button named "confim". If it cant find him, it will return the value "0" (if i understood correctly the help file).

So: If oForm = 0 then that means that there is no confirm button, THEREFORE that means that this is the message "the entry is already in the system" and that, sconsequently, there is there "ok" button (whose value is "action"), and it has to click on it.

If the value of oForm isnt zero, that means that there isnt the "the entry is already in the system" and that it has to click on the "confirm" button.

CODE

Else

$oForm = _IEGetObjByName ($oIE, "confirm")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

Endif

However, the elements in the drop down menu keep changing. Therefore, i cant use the "ByText" option to select them. This is why i wanted to use the ByIndex option, but seriously, i dont understand how this otpion works... What is a 0 index? i thought that meant that first element is #0 then #1 then #2 and so on...

Could you help me with that ?

And on the other hand, since i wont use the ByText option, how can i do to be sure, not to pick up the next option at the next cycle. Cause, if the entry is already made, its not removed from the drop down menu. But its not already made, then it doesnt appear anymore after its entered... A bit complicated isnt it ?

Maybe, (this is just an idea), we could save the element name in something (maybe this is what you call variable, i dont know) and then, at the next cycle, make it check if the element it is picking doesnt match with the name of the elements which are in the variable... Still a bit complicated :)

Do u get it ?

Link to comment
Share on other sites

Well yes, 0:First element,1:Second element,...,n: n+1 element.

I don't have much time now, but take a look at this example, might give you ideas:

#include <IE.au3>
$oIE = _IE_Example ("form")
$oForm = _IEFormGetObjByName ($oIE, "ExampleForm")
$oSelect = _IEFormElementGetObjByName ($oForm, "selectExample")
_IEFormElementOptionselect ($oSelect, 1, 1, "byIndex")
$Value=_IEFormElementGetValue ($oSelect)
MsgBox(0,"",$Value,1)
_IEFormElementOptionselect ($oSelect, 0, 1, "byIndex")
$Value=_IEFormElementGetValue ($oSelect)
MsgBox(0,"",$Value,1)

_IEFormElementGetValue returns the value of a given Form Element.

Link to comment
Share on other sites

im sorry, i dont get it...

$oSelect = _IEFormElementGetObjByName ($oForm, "selectExample")

What does this line do ?

I did that to mimic the example :

$oForm = _IEFormGetObjByName ($oIE, "planid")
$oSelect = _IEFormElementGetObjByName ($oForm, "first_entry_of_the_drop_down_menu")
_IEFormElementOptionselect ($oSelect, 2, 1, "byIndex")

But that still picks up the first entry. Even if i changed the "1" into a "2" or whatever else. <_<

Link to comment
Share on other sites

i think that the problem comes from the name of the dropdown menu :

<form action=menu method=post><SELECT name=planid size=1><OPTION VALUE=145702>aaa </OPTION><OPTION VALUE=151720> bbb </OPTION><OPTION VALUE=149966>ccc </OPTION>

How can i get it as an object ? i did

$oForm = _IEFormGetObjByName ($oIE, "planid")

but that doesnt work

i have also tried

$oForm = _IEFormGetObjByName ($oIE, "menu")

but, same sad result.

any idea please ? <_<

EDIT : ok, so the problem is apparently that my form doesnt have a name... How can i bypass that ? there is no command _IEFormGetObjByAction :/

Edited by Koopa
Link to comment
Share on other sites

well. spent two hours on it again, and i found a way to identify my form.

Now, and please, help me on this one cause the help file doesnt really explain it, how can i extract all the element of my drop down menu as text, and create a loop that pick up at each time one of the text elements ?

Thanks for your help...

EDIT : here is the latest version of my code to enable you to help me more easily :

#include <IE.au3>

$oIE = _IECreate ("http://www.xxxxxxxxxxxxxxxxx")

For $i = 1 To 20

$oForm = _IEGetObjByName ($oIE, "newentry")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

$oForm = _IEFormGetCollection ($oIE, 0)

$oQuery = _IEFormElementGetCollection ($oForm, 1)

$oSelect = _IEFormElementGetObjByName ($oForm, "planid")

_IEFormElementOptionselect ($oSelect, $i, 1, "byIndex")

$oForm = _IEGetObjByName ($oIE, "newentry")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

$oForm = _IEGetObjByName ($oIE, "newentry")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

$oForm = _IEGetObjByName ($oIE, "action")

_IEAction ($oForm, "click")

_IELoadWait ($oIE)

Next

Note : it works perfectly as it is. However, i need the feature i explained at the beginning. Edentifying elements as text and pick one up, at each loop.

Edited by Koopa
Link to comment
Share on other sites

This is the clreaest example I can make. Study how it works, check the source code of pages 1, 2 and 3 to see how to get the names and what values it's taking.

#include <IE.au3>
Dim $Selected[4]
;Create the explorer
$oIE =_IECreate("http://losprofes.110mb.com/test/page.html")


;Get the form
$oForm = _IEFormGetObjByName ($oIE, "myform")
;Get the dropdown
$oSelect = _IEFormElementGetObjByName ($oForm, "mydropdown")
;Select 2nd item in dropdown.
_IEFormElementOptionselect ($oSelect, 1, 1, "byIndex")
;Get the value of the selected item
$Selected[0]= _IEFormElementGetValue($oSelect)
;Show you the value
MsgBox(0,"","Selected: "&$Selected[0])
;Go to the next page
_IELinkClickByText($oIE ,"Go to page 2")
;Wait.
_IELoadWait($oIE)


;Get the form
$oForm = _IEFormGetObjByName ($oIE, "myform")
;Get the dropdown
$oSelect = _IEFormElementGetObjByName ($oForm, "mydropdown")
;Select 1st item of the dropdown
_IEFormElementOptionselect ($oSelect, 0, 1, "byIndex")
;Get value of the selected item
$Selected[1]= _IEFormElementGetValue($oSelect)
;Show you the value
MsgBox(0,"","Selected: "&$Selected[1])
;Go to the next page
_IELinkClickByText($oIE ,"Go to page 3")
;Wait.
_IELoadWait($oIE)


;Get the form
$oForm = _IEFormGetObjByName ($oIE, "myform")
;Get the dropdown
$oSelect = _IEFormElementGetObjByName ($oForm, "mydropdown")

;Loop to check if the item that will be selected now has already been taken
For $i=0 To UBound($Selected)
    ;Will select an option in the dropdown every loop
    _IEFormElementOptionselect ($oSelect, $i, 1, "byIndex")
    ;Will exit once the array $Selected is empty
    If $Selected[$i]="" Then ExitLoop
    ;Will get the value of the selected option in the previous step
    $Check=_IEFormElementGetValue($oSelect)
    ;Loop to check if the value of the selected option matches one in the array
    For $a=0 To UBound($Selected)
        If $Check=$Selected[$a] Then
            MsgBox(0,"","The item "&$Check&" has been previously selected. Picking next one.")
            ExitLoop
        EndIf
    Next
Next

MsgBox(0,"","The selected item is: "&_IEFormElementGetValue($oSelect)&". This item has not been selected before.")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...