[SOLVED] How to tell if a DOM object exists on a webpage

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
momo2000
Posts: 32
Joined: 25 Jan 2015, 12:00

[SOLVED] How to tell if a DOM object exists on a webpage

15 Aug 2015, 09:17

I’ve been working with DOM to be able to automatically populate the fields on a web page. I used iWB2 Learner to get the names of the fields and it is going well, and I have been able to access and populate/write all the input fields, such as: (using wb := IEGet("Calendar - Bookeo") from Jethreow)

Code: Select all

wb.Document.All.cardType.Value := "VISADEBIT"   
wb.Document.All.cardNumber.Value := "1234123412341234"
wb.Document.All.cardHolderName.Value := "John Smith"
wb.Document.All.expiryMonth.Value := "12"
wb.Document.All.expiryYear.Value := "2021"
wb.Document.All.cvv.Value := "999"
However, I am trying to add a bit of error checking into the code and “check” and see if the user is on the correct page, before allowing the script to proceed further. Using the “handle” wb := IEGet("Calendar - Bookeo") and checking to see if it is valid won’t work because there are several screens/modes on the webpage that still have that as a title, so I really need to check and see if the input box exists.

However, I can’t seem to get the syntax correct. I’ve tried using the index number (although I’d rather check the actual “name” since I’m worried that the index number might change).

Code: Select all

if wb.Document.All.cardNumber.index = 120
{
msgbox, found object	
}
How could I check the existence of an input box on a page (hopefully using the input box name)?
Last edited by momo2000 on 22 Aug 2015, 13:07, edited 1 time in total.
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: How to tell if a DOM object exists on a webpage

15 Aug 2015, 11:33

If your using IE9+ one way you can use is getElementsByName()

Like this

Code: Select all

if (wb.Document.getElementsByName("cardType").length > 0)
{
msgbox, found object	
}
This topic has more ways http://stackoverflow.com/questions/5629 ... isible-dom
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
momo2000
Posts: 32
Joined: 25 Jan 2015, 12:00

Re: How to tell if a DOM object exists on a webpage

16 Aug 2015, 14:01

paymentscreen.png
Thanks, Blackholyman: I was out yesterday, but am back on this today.

I understand what you are saying in your example and that should work perfectly, because the “cardtype” always has a value (can’t be blank), so that would be a good thing to check the existence of. So if that object exists, then the code can assume that the user is on the correct page/form.

However, I can’t seem to make that syntax work. The msgbox message never appears, so I assume the syntax isn’t correct, or I’m missing something. I have attached a screen shot of the webpage, and also the iWB2 Learner as well.

I tried both:

Code: Select all

if (wb.Document.getElementsByName("cardType").length > 0)
and

Code: Select all

if (wb.Document.getElementsByName("customerCreditCardInput.cardType ").length > 0)
Any thoughts?
momo2000
Posts: 32
Joined: 25 Jan 2015, 12:00

Re: How to tell if a DOM object exists on a webpage

16 Aug 2015, 23:09

Been working with this more today.
If I am on the correct web page, and run this code it changes the value of the "cardtype" control:

Code: Select all

wb.Document.All.cardType.Value := "VISADEBIT"
If I am NOT on the correct page and run that code, I get an error (that stops AHK): "Error 0x80020006 - Unknown name." SO, how can I check for the EXISTANCE of the object BEFORE I change its value. That way if it doesn't exist, I can throw a nice error (MsgBox) to the user, instead of running code that will crash AHK?
momo2000
Posts: 32
Joined: 25 Jan 2015, 12:00

Re: How to tell if a DOM object exists on a webpage

17 Aug 2015, 13:33

Thanks evilC: Never used TRY before. But I am "trying" :) I used this code:

Code: Select all

wb := IEGet("Calendar - Bookeo") 
try
{
    wb.Document.All.cardType.Value := "VISADEBIT"
}
catch
{
    MsgBox, You are not on the correct page
}
But it doesn't "catch" and show the msg box . . . even with my web browser completely closed . . . strange. I must be doing something wrong.
momo2000
Posts: 32
Joined: 25 Jan 2015, 12:00

Re: How to tell if a DOM object exists on a webpage

22 Aug 2015, 13:06

I did get this working using TRY. Good idea. So now I just "try" to interact with the object, and if I get an error, then I assume they are on the wrong page. Took me a bit to figure it out, but this method works just fine for my purposes. Thanks for the suggestion, wouldn't have figured it out on my own

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput and 347 guests