Jump to content

Array variable has incorrect number of subscripts or subscript dimension range exceeded


Go to solution Solved by Danp2,

Recommended Posts

I have a script that I have created that loops through an html table with pagination. The html table has 10 links that start with link1, link2, link3, etc.. The script clicks on each of these links. After link11 is clicked, the script then proceeds to click on the "Next" button in the pagination and the process repeats again for each link on the 2nd page, 3rd page, 4th page, etc....However, when it reaches page 12 it loops fine until it completes the last link in the page. Any help is greatly appreciated. I receive the following error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Here is my html pagination table

<table class="table" cellspacing="0">
  <thead>
    <tr>
      <th>Test ID</th>
      <th>Link</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>

    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a href="#link1">1</a></td>
      <td><a href="#link1">link1</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr class="">
      <td><a href="link2">2</a></td>
      <td><a href="#link2">link2</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr>
      <td><a href="#link3">3</a></td>
      <td><a href="#link3">link3</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr class="">
      <td><a href="#link4">4</a></td>
      <td><a href="#link4">link4</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr>
      <td><a href="#link5">5</a></td>
      <td><a href="#link5">link5</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr class="">
      <td><a href="#link6">6</a></td>
      <td><a href="#link6">link6</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr>
      <td><a href="#link7">7</a></td>
      <td><a href="#link7">link7</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr class="">
      <td><a href="#link8">8</a></td>
      <td><a href="#link8">link8</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr>
      <td><a href="#link9">9</a></td>
      <td><a href="#link9">link9</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr class="">
      <td><a href="#link10">10</a></td>
      <td><a href="#link10">link10</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
    <tr>
      <td><a href="#link11">11</a></td>
      <td><a href="#link11">link11</a></td>
      <td>None</td>
      <td>Test</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      
      
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th colspan="10"><div style="float: center">
          <div class="tablePagination"><span class="previousPage disabled">Previous</span> 1 <a href="#2">2</a> <a href="#3">3</a> <a href="#4">4</a> <a href="#5">5</a> <a href="#6">6</a> <a href="#7">7</a> <a href="#8">8</a> <a href="#9">9</a> <a href="#10">10</a> <a href="#11">11</a> <a href="#12">12</a> <a class="nextPage" href="#next">Next</a></div>
        </div></th>
    </tr>
  </tfoot>
</table>

Autoit script

#Include "wd_core.au3"
#Include "wd_helper.au3"
#Include "WinHttp.au3"
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Local $sDesiredCapabilities, $sSession
SetupChrome()

_WD_Startup()

$sSession = _WD_CreateSession($sDesiredCapabilities)

_WD_LoadWait($sSession)

_WD_Navigate($sSession, "pagination.html")

_WD_LoadWait($sSession)

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//table")
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//tbody")
Local $aArray1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a", $sElement, True)

sleep(5000)

LoopPagination()

Func LoopPagination()

For $i = 0 to UBound($aArray1) - 1

    $aArray1[$i] = _WD_ElementAction($sSession, $aArray1[$i], 'click')


EndFunc

NextPage()

Func NextPage()
sleep(8000)

EndFunc

Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"debuggerAddress": "localhost:9222"} {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}'
EndFunc

 

Edited by goku200
Link to post
Share on other sites

Are you aware that you have a nested for-next loop with both loops controlled by the same variable $i. ? This can only lead to disaster.

I can't make out where your subscript error comes from, you have not reported which line is generating the error.

Phil Seakins

Link to post
Share on other sites

I went ahead and corrected the code to only include the one loop.

$test = _WD_ElementAction($sSession, $aArray2[$i], 'click')

The error occurs on this line:

$test = _WD_ElementAction($sSession, $aArray2[$i], 'click')

 

Link to post
Share on other sites
1 hour ago, goku200 said:

  $test = _WD_ElementAction($sSession, $aArray2[$i], 'click')

Your acting as if $aArray2 will have at least as many rows as $aArray1.

Because $i is iterating based on $aArray1 size, but being used to reference $aArray2.

Code hard, but don’t hard code...

Link to post
Share on other sites

 

When I changed it to $aArray1[$i], the script clicks on the link but when it goes back it does not click on the next link…

  $test = _WD_ElementAction($sSession, $aArray1[$i],'click')

Link to post
Share on other sites
Quote

    _WD_ExecuteScript($sSession, 'return history.back()')

Did you try using _WD_Action here?

Quote

    $aArray1[$i] = _WD_ElementAction($sSession, $aArray1[$i], 'text')
    
    Local $aArray2 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, ".//td[2]/a", $aArray1, True)

I'm not sure how this is supposed to work. If $aArray1[$i] already holds an element ID, why are you replacing it with the element's text and then attempting to use it as a starting element?

I suggest that you repost your revised script so that we can better assist you.

Link to post
Share on other sites
Posted (edited)
#Include "wd_core.au3"
#Include "wd_helper.au3"
#Include "WinHttp.au3"
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Local $sDesiredCapabilities, $sSession
SetupChrome()

_WD_Startup()

$sSession = _WD_CreateSession($sDesiredCapabilities)

_WD_LoadWait($sSession)

_WD_Navigate($sSession, "pagination.html")

_WD_LoadWait($sSession)

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//table")
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//tbody")
Local $aArray1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a", $sElement, True)

sleep(5000)

LoopPagination()

Func LoopPagination()

For $i = 0 to UBound($aArray1) - 1

    $aArray1[$i] = _WD_ElementAction($sSession, $aArray1[$i], 'click')


EndFunc

NextPage()

Func NextPage()
sleep(8000)

EndFunc

Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"debuggerAddress": "localhost:9222"} {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}'
EndFunc

Thank you @Danp2 Here is the code again with the corrections that I've made.

 

Edited by goku200
Link to post
Share on other sites
  • Solution

Here's my feedback --

  • You've got a While loop in NextPage that shouldn't be there
  • The While loop should be in your main script
  • Inside this While loop, you should --
    • Load $aArray1 with the desired links
    • Call the LoopPagination function to process the links
    • Call the NextPage function to switch to the next page
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...