Jump to content

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


Recommended Posts

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

Link to comment
Share on other sites

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.
Link to comment
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! 

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...