Sign in to follow this  
Followers 0
fcp123

Binding keys to elements whose id is unpredictable and which have no name?

4 posts in this topic

Hello all,

I am working on a relatively simple project and could use a little help.

I'm trying to use HotKeySet to bind keys to the different buttons on web-based multiple choice question form. Currently I am using the mouse to click my answer choice and the submit button and it's getting really tiresome (I'm doing thousands of these questions). Ordinarily, I would just use _IEGetObjectbyId, but as you can see below the answer choices do not have predictable ids nor a name to reference. My first thought was to reference them by the order in which they appear on the page as they have a constant div class. Is there a way to do that? Please see the attachment for a screenshot.

Element Inspector:

Answer choice A:

<div class="answer amplifire-question-answer-ct x-component" id=x-auto-64">

Answer choice B:

<div class="answer amplifire-question-answer-ct x-component" id=x-auto-68">

Answer choice C:

<div class="answer amplifire-question-answer-ct x-component" id=x-auto-72">

Thanks!

-Fred

post-81020-0-49599000-1374035710_thumb.j

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't think the IE DOM allows for wild cards, but you can try it anyways (_ie functions).  Else, you can do a loop, and logically grab all the div's where you itterate a counter that's appended to x-auto- (skip those that don't return an object using IsObj)...using _IEGetObjById

Else, one of my links, below can work.

where the XPath passed in to the function would look like:

"//div[contains(@id,'x-auto-')]"

the function returns an array of all matches...depending, you might need to be more specific, like:

"//div[contains(@id,'x-auto-') and @class='answer amplifire-question-answer-ct x-component']"

whether you use my function (array), or the _IE* function (object collection), you can loop through and make your hotkeyset

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

jdelaney, 

Thank you for your help (and your UDF). I'll spend some time tinkering with what you suggested and post the results.

Share this post


Link to post
Share on other sites

#cs
    This script allows the user to submit answers and move to the next question in BarBri Amp by using the space bar. This is an alternative to using the mouse to click on the buttons.
    To use this script, open BarBri Amp to a question page, start the script, select an answer, press space bar to submit answer and space bar again to move to the next question.
    Created 7.17.13
#ce

#include <IE.au3>

MsgBox(0, "Instructions", "1. Make sure BarBri Amp is open to a question page before starting script." & @CRLF & "2. Script must be restarted for each new Amp lesson." & @CRLF & "" & @CRLF & "Space bar: submits answer and also moves to next question." & @CRLF & "Esc key: exits script.")

;bind keys to functions
HotKeySet("{Space}", "Answer")
HotKeySet("{Esc}", "Terminate")

;detect BarBri Amp window and get button object
$oIE = _IEAttach("Learning")
$oAnsSubmit = _IEGetObjById($oIE, "ANSWER_SUBMIT_PANEL_ACTION_BUTTON")

;check for BarBri Amp question page
If $oAnsSubmit = 0 Then ;if returns @NoMatch, exit script
    MsgBox(0, "", "BarBri Amp is not open to question page. Script will now exit. Please open BarBri Amp to a question page and restart script.")
    Exit 0
EndIf

;loop script
While 1

WEnd

Func Answer()
    _IEAction($oAnsSubmit, "click")
EndFunc   ;==>Answer

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Well, I decided to just use the mouse to select the answer, but to use the space bar to submit answers and move to the next question. Even though I decided not to pursue what my original question was asking, I would appreciate feedback on my code. I'm always interested in learning better ways to code. Thanks! 

Share this post


Link to post
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
Sign in to follow this  
Followers 0