How to store a changing webpage in a variable? - windows

My script automates the room booking process at my school for group projects. I have created an automatic log-in script which works fine. Now I would like to access different elements from the loaded page (check boxes, radio buttons...).
How can I save various elements from the page that I have logged-in into and perform certain actions on them?
Func SignIn()
Global $window = _IECreate("https://roombooking.au.dk/classes/Login.aspx? ReturnUrl=%2fclasses%2fbook.aspx")
_IELoadWait($window)
If #error Then Return
WinSetState("[ACTIVE]", "", #SW_MAXIMIZE)
Local $username = _IEGetObjByName($window,"ctl00$Main$UsernameBox")
Local $password = _IEGetObjByName($window,"ctl00$Main$PasswordBox")
Local $button = _IEGetObjByName($window, "ctl00$Main$LoginBtn")
_IEFormElementSetValue($username,"abc")
_IEFormElementSetValue($password,"123")
_IEAction ($button, "click")
EndFunc
Func Room()
Local $SelectRoom = _IEGetObjByName(**???**,"ctl00$Main$ChangeReqsBtn")
_IELoadWait($bwindow)
_IEAction($s526,"click")
EndFunc

From the Help File:
#include <IE.au3>
_IEGetObjByName ( ByRef $oObject, $sName [, $iIndex = 0] )
$oObject Object variable of an InternetExplorer.Application, Window or Frame object
$sName Specifies name of the object you wish to match
$iIndex If name occurs more than once, specifies instance by 0-based index
0 (Default) or positive integer returns an indexed instance
-1 returns a collection of the specified objects
In your case the code will be something like:
Local $SelectRoom =
_IEGetObjByName($window,"ctl00$Main$ChangeReqsBtn")

AutoIt offers many different approaches to HTML document retrieval. Without providing concerning source code it can only be guessed.
HTML source of document is returned by _IEDocReadHTML() (assuming you're using IE.au3 UDF). Example:
#include <IE.au3>
Global Const $oIE = _IECreate('http://www.google.com/')
Global Const $sDocHTML = _IEDocReadHTML($oIE)
_IEQuit($oIE)
ConsoleWrite($sDocHTML & #LF)
Exit 0
Mentioned UDF contains functions to set values to form elements (lookup _IEForm...() in AutoIt's user defined function reference).

Related

Enterprise Architect: Populating enum programmatically, but they're being alphabetized and not in the order they're entered

I'm writing a VBScript script to assist in laying out the architecture of some software I'm developing at work. I'm parsing the header files for defined enumerations, structs, function definitions, and some auto-generated commented stuff related to the categorical structure of our modules.
It's all going swimmingly, and I can create an appropriate package (we categorize our modules by layer level, i.e. drivers vs application level, etc), add a Component, then populate the Component with a blank Diagram, Enumerations for state and error, an Interface for methods, and a struct for the data structure.
My problem is coming in when I try to populate the Enumerations with Attributes. When I do it by hand, they're listed in the proper order (where they would resolve to 0, 1, etc.), but when I populate them while parsing the header file, they get entered alphabetically, which is obviously a problem.
I tried modifying the Default member of each Attribute with it's corresponding numerical value, but it still alphabetized them (and added the numerical value, too).
Here's a snippet of my code. Hopefully it's sufficient!
set newStateEnum = newComponent.Elements.AddNew(fileName & "_state_t", "Enumeration")
dim tempState, initialValue
dim tempAttribute as EA.Attribute
initialValue = 0
for each tempState in stateParts
if InStr(tempState, "=") > 0 then
set tempAttribute = newStateEnum.Attributes.AddNew(Split(tempState, "=")(0), "enum")
else
set tempAttribute = newStateEnum.Attributes.AddNew(Split(tempState, ",")(0), "enum")
end if
tempAttribute.Default = initialValue
initialValue = initialValue + 1
tempAttribute.Update()
next
newStateEnum.Update()
You can use EA.Attribute.Pos to set a custom ordering.
So something like:
for each tempState in stateParts
if InStr(tempState, "=") > 0 then
set tempAttribute = newStateEnum.Attributes.AddNew(Split(tempState, "=")(0), "enum")
else
set tempAttribute = newStateEnum.Attributes.AddNew(Split(tempState, ",")(0), "enum")
end if
tempAttribute.Default = initialValue
initialValue = initialValue + 1
tempAttribute.Pos = initialValue 'set the custom POS here
tempAttribute.Update()
next
Make sure you allow custom attribute ordering in the settings, or this won't have any visible effect either.

JavaEdit search function not locating visible element

I've got two library functions:
Function searchWindow(title)
Set searchWindow = Window("title:=" + title)
End Function
And
Function searchField(label)
Set searchField = JavaEdit("attached text:=" + label)
End Function
Here I'm testing them:
Environment.Loadfromfile("C:\UFTConstants\constants.ini")
Set loginFrame = searchWindow(Environment.Value("frameLogin"))
loginFrame.Click
Set userField = searchField("User ID / Ci-Usager")
userField.Set "test"
The first function works fine, and it's title property matches that of the application. However, the second will not find the text field, despite properties matching:
The error:
I've tried other properties as well, tagname, various class properties, as well as combinations of all three, and none are producing a find.
Any ideas?
First Update
As per request, full spy screencap:
The full line generated by the recording tool:
JavaWindow("Application Name").JavaDialog("Window Title").JavaEdit("User ID / Ci-Usager").Set "user"
However, when I try to re-create this programmatically, I get the same error, only for JavaWindow instead:
"Cannot identify the object [JavaWindow] of (class JavaWindow)..."
Possible Java set-up issue? This would not explain why recording can still locate Java objects, however.
Second Update
Here are the recognition properties:
I have ensured that all properties are set, still unable to locate.
Final Update
Ok, I've reduced the code to absolute barebones. No external constant file, no external library calls. I've copied the full extent of what is recorded in recording mode. I've printed each variable to ensure accuracy. I've included the full object hierarchy:
Set objWin = JavaWindow("label:=<redacted>")
objWin.SetTOProperty "to_class", "JavaWindow"
objWin.SetTOProperty "toolkit class", "javax.swing.JFrame"
MsgBox objWin.GetTOProperty("label")
MsgBox objWin.GetTOProperty("to_class")
MsgBox objWin.GetTOProperty("toolkit class")
Set objDialog = objWin.JavaDialog("label:=<redacted>")
objDialog.SetTOProperty "to_class", "JavaDialog"
objDialog.SetTOProperty "toolkit class", "<redacted>.LoginDialog"
MsgBox objDialog.GetTOProperty("label")
MsgBox objDialog.GetTOProperty("to_class")
MsgBox objDialog.GetTOProperty("toolkit class")
Set objEdit = objDialog.JavaEdit("attached text:=User ID / Ci-Usager")
objEdit.SetTOProperty "to_class", "JavaEdit"
objEdit.SetTOProperty "toolkit class", "javax.swing.JTextField"
MsgBox objEdit.GetTOProperty("attached text")
MsgBox objEdit.GetTOProperty("to_class")
MsgBox objEdit.GetTOProperty("toolkit class")
objEdit.Set "test"
Note the redacted text is to remove identifying elements from the code. They have been triple-checked on my side and are correct.
This still does not work.
However, recording the same does. What gives?
I think you have to mention the full hierarchy while working with the Javaedit field. Try re-writing the code for the function searchField as :
Function searchField(label)
Dim objFrame
Set objFrame = searchWindow(Environment.Value("frameLogin"))
Set searchField = objFrame.JavaEdit("attached text:=" + label) 'Javaedit should be the child of the login window. You had to mention the full hierarchy here
End Function

Using JobQueue to continuously refresh a message

I'm building a Telegram bot that uses ConversationHandler to prompt the user for a few parameters and settings about how the bot should behave. This information is stored in some global variables since it needs to be available and editable by different functions inside the program. Every global variable is a dictionary in which each user is associated with its own value. Here's an example:
language = {123456: 'English', 789012: 'Italian'}
where 123456 and 789012 are user ids obtained from update.message.from_user.id inside each function.
After all the required information has been received and stored, the bot should send a message containing a text fetched from a web page; the text on the web page is constantly refreshed, so I want the message to be edited every 60 seconds and updated with the new text, until the user sends the command /stop.
The first solution that came to my mind in order to achieve this was something like
info_message = bot.sendMessage(update.message.chat_id, text = "This message will be updated...")
...
def update_message(bot, update):
while True:
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
time.sleep(60)
Of course that wouldn't work at all, and it is a really bad idea. I found out that the JobQueue extension would be what I need. However, there is something I can't figure out.
With JobQueue I would have to set up a callback function for my job. In my case, the function would be
def update_message(bot, job):
url = "http://example.com/etc/" + language[update.message.from_user.id]
result = requests.get(url).content
bot.editMessageText(result, chat_id = update.message.chat_id, message_id = info_message.message_id)
and it would be called every 60 seconds. However this wouldn't work either. Indeed, the update parameter is needed inside the function in order to fetch the page according to the user settings and to send the message to the correct chat_id. I'd need to pass that parameter to the function along with bot, job, but that doesn't seem to be possible.
Otherwise I would have to make update a global variable, but I thought there must be a better solution. Any thoughts? Thanks.
I had the same issue. A little digging into the docs revealed that you can pass job objects a context parameter which can then be accessed by the callback function as job.context.
context (Optional[object]) – Additional data needed for the callback function. Can be accessed through job.context in the callback. Defaults to None
global language
language = {123456: 'English', 789012: 'Italian'}
j=updater.job_queue
context={"chat_id":456754, "from_user_id":123456, "message_id":111213}
update_job = job(update_message, 60, repeat=True, context=context)
j.put(update_job, next_t=0.0)
def update_message(bot, job):
global language
context=job.context
url = "http://example.com/etc/" + language[context["from_user_id"]]
result = requests.get(url).content
bot.editMessageText(result,
chat_id = context["chat_id"],
message_id = context["message_id"])

ArcPad - VBscript - Autopopulate attributes

I am using the following script to grab parcel and address information from one layer to fill the attribute table of a newly created feature.
There is no returned error, but the problem I am having is that there seems to be the wrong information stuck in the memory of recordselect function. No matter where I place a point it gives the same parcel # and address. Or maybe it isn’t actually be performing the IF function properly.
Sub Address
Dim rsCurrentXY
Set rsCurrentXY = Map.Layers("Violations").records
rsCurrentXY.movelast
Dim objXYShape
Set objXYShape = rsCurrentXY.Fields.Shape
Dim pControls
Set pControls= Application.Map.selectionlayer.Forms("EDITFORM").Pages(“PAGE1”).Controls
Dim rsGrid
' Find corresponding map page to the valve point
Set rsGrid = Map.Layers("ACPA_parcels").records
rsGrid.movefirst
Do While Not rsGrid.eof
If rsGrid.fields.shape.Ispointin(objXYShape) Then
pControls("txtAddress").value = rsGrid.Fields("ADD1").Value
Exit Do
End If
rsGrid.Movenext
Loop
' Clean Up
Set rsCurrentXY = Nothing
Set objXYShape = Nothing
Set rsGrid = Nothing
End Sub
(I have another subroutine called "PIN" that would do the exact same thing.)
I have them called when their respective edit boxes in the custom form are activated by the inspector.
Thanks for the help,
Robert
Accessing the EDITFORM via Application.Map.selectionlayer.Forms("EDITFORM") will be problematic. Whenever working with controls on an EDITFORM you should using ThisEvent.Object to discover all your objects. For example, if your event handler is Page_OnLoad then ThisEvent.Object will refer to your current page. You should have code like this:
Dim pPage1
Set pPage1 = ThisEvent.Object
Dim pControls
Set pControls = pPage1.Controls

VB6 how to get the selected/checked control in a control array

I have to modify a VB6 app and am repeatedly beating my head against a wall over control arrays.
I know that the event handler for the array includes its index value and I can set some variable there, but i should be able to directly access the selected radio button in an array of OptionButton. Currently I'm doing this
For i = 0 To optView.Count - 1
If optView.Item(i).value = True Then
currIndex = i
Exit For
End If
Next
Is this really my only option?
Yes, this is our only option. The control array object does not contain any selecting logic (which makes sense, as "selected" might mean different things for different controls). The only change I'd make is replacing the For with For Each.
Another way to do this that I have used. Write a function, and then call the function, passing in the control name, to return the index number. Then you can reuse this in the future especially, if you add it to a module (.bas).
Function f_GetOptionFromControlArray(opts As Object) As Integer
' From http://support.microsoft.com/KB/147673
' This function can be called like this:
' myVariable = f_GetOptionFromControlArray(optMyButtons) 'Control syntax OK
' myVariable = f_GetOptionFromControlArray(optMyButtons()) 'Array syntax OK
On Error GoTo GetOptionFail
Dim opt As OptionButton
For Each opt In opts
If opt.Value Then
f_GetOptionFromControlArray = opt.Index
Exit Function
End If
Next
GetOptionFail:
f_GetOptionFromControlArray = -1
End Function

Resources