Jump to content

_WD_SelectFiles uploads the same images 4 times

Recommended Posts

I have a script that I have created that's supposed to upload images using _WD_SelectFiles. The looping of my script works as expexted, however it uploads the same image for each image 4 times. It then loops to the next image, but it continues to upload it 4 times of the same name image. Below is my script. Any help is greatly appreciated 🙂

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

Local $sDesiredCapabilities, $sSession
$sSession = _WD_CreateSession($sDesiredCapabilities)


_WD_Navigate($sSession, "")


Local $aList = _FileListToArrayRec("C:\Test\images\", "*|*.db", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)


For $i = UBound($aList) - 1 To 0 Step - 1
    _WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file1']", @ScriptDir & "\images\" & $aList[$i])
    _WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file2']", @ScriptDir & "\images\" & $aList[$i])
    _WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file3']", @ScriptDir & "\images\" & $aList[$i])
    _WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file4']", @ScriptDir & "\images\" & $aList[$i])



Func SetupChrome()

    _WD_Option('Driver', 'chromedriver.exe')

    _WD_Option('Port', 9515)

    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

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





Edited by goku200
Link to post
Share on other sites

You are assigning the same value ($aList[$i]) to each of the input elements. Therefore, the same image gets uploaded four times just like you told it to do. 😄

Your loop code will need to be rewritten so that it --

  • assigns different values to each element
  • uses a different step value
  • makes sure it doesn't exceed array size (ie: if you start using $i+1, $i+2, etc)
Link to post
Share on other sites

@Danp2Thanks for the reply. Are you saying change:

_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file1']", @ScriptDir & "\images\" & $aList[$i])


_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file1']", @ScriptDir & "\images\" & $i + 1)

A little confused as to what I need to change 🙂

Edited by goku200
Link to post
Share on other sites

Untested, but I was suggesting that you change your code to something like this --

$iMax = UBound($aList) - 1

For $i = 0 To $iMax Step 4
    For $j = 0 To 3
        $iIndex = $i + $j
        If $iIndex > $iMax Then ExitLoop 2
        _WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='file" & $j + 1 & "']", @ScriptDir & "\images\" & $aList[$iIndex])


Link to post
Share on other sites

Thank you @Danp2. Quick question is there a reason why it doesn't select the the first upload input? It goes straight to the second input, third input, fourth input..... and then when it re-loops it selects the first input for the remaining image in that folder to be uploaded

Output says File Not Found: C:\\Test\\images\\

Link to post
Share on other sites

You could try adding some calls to ConsoleWrite throughout the code to help you troubleshoot it. You could also modify the line with _FileListToArrayRec to use $FLTAR_FULLPATH for the return path. Then you can eliminate the following portion of code and just use the raw contents of the array --

@ScriptDir & "\images\"


I took a closer look at the array created by _FileListToArrayRec, and the first entry contains the number of returned entries. Therefore, you need to change the line

$iIndex = $i + $j


$iIndex = $i + $j + 1

so that you start processing the array at position 1 instead of 0.

Edited by Danp2
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...