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

 

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

  • Solution
; 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
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

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

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

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