Jump to content

Subscript used on non-accessible variable (Webdriver)


Go to solution Solved by seadoggie01,

Recommended Posts

I have seen some similar post, but not sure how to implement using Web-driver.

So my problem is I am looking for all instances of the word "yes" on my webpage. (chrome)

The problem is "yes" may be there once or even not at all. 

I understand why I am getting the error. Because it is not always a array. However, the error is causing my script to end. I don't want it to end the query. I just want it to continue on. 

Here is an example: This will click the first and second instance of "yes' on the webpage. (as long as it exists)

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[contains(text(),'Yes')]","",True) 
_WD_ElementAction($sSession, $sElement[0], 'click')
    Sleep (2000)
_WD_ElementAction($sSession, $sElement[1], 'click')

This is the error I am getting. 

"C:\Users\******\Desktop\Expanse Auto Reg Test.au3" (44) : ==> Subscript used on non-accessible variable.:
_WD_ElementAction($sSession, $sElement[0], 'click')
_WD_ElementAction($sSession, $sElement^ ERROR
->14:41:54 AutoIt3.exe ended.rc:1
+>14:41:54 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 21.74

 

Life's simple. You make choices and you don't look back.

Link to post
Share on other sites
  • Solution
Posted (edited)
; Note that we usually say 'a' before array type variables
Local $aElement = _WD_FindElement(...)
; For each element in the array
For $i=0 To UBound($aElement) - 1
    ; Perform the action on that element, we access it with $i, which goes from 0, 1, 2, etc until the size of the array
    _WD_ElementAction($sSession, $aElement[$i], "click")
Next

Edit: You should probably be checking for @error after each _WD_ call if you aren't already

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites
Posted (edited)

I had to add some delay. probably a little overkill but it works. 

Thank you both!

Local $aElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[contains(text(),'Yes')]","",True)
; For each element in the array
For $i=0 To UBound($aElement) - 1
    ; Perform the action on that element, we access it with $i, which goes from 0, 1, 2, etc until the size of the array
    _WD_ElementAction($sSession, $aElement[$i], "click")
        If @error <> $_WD_ERROR_Success Then MsgBox(0, "Error","Could not click 'Yes' Button " & $i)
      Sleep (2000)
Next

 

Edited by SkysLastChance

Life's simple. You make choices and you don't look back.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...