Jump to content

Array only writes to one Excel cell when an element is found


goku200
 Share

Recommended Posts

I'm having some issues with writing to column C when an element is found. It works on C2 but it does not continue to C3, C4, C5, etc..... I'm wanting to write "test" if the element //input[@id='username'] is found  $someUser = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='username']"). I have attached my HTML and Excel file along with my AutoIt code below:

#Include "wd_core.au3"
#Include "wd_helper.au3"
#Include "wd_core.au3"
#Include "File.au3"
#Include "Array.au3"
#Include "Excel.au3"


Local $sDesiredCapabilities, $sSession
_WD_Startup()

$Ssession = _WD_CreateSession($sDesiredCapabilities)

_WD_Navigate($sSession, "https://127.0.0.1/test.html")

_WD_LoadWait($sSession)

Local $oExcel = _Excel_Open()

Local $oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\<Username>\Downloads\test.xlsx")

Local $aArrayTest1 = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"))
Local $aArrayTest2 = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.ActiveSheet.Usedrange.Columns("B:B"))


For $i = 0 To UBound($aArrayTest1) - 1 

    _WD_Navigate($Ssession, $aArrayTest1[$i])
    
    _WD_LoadWait($sSession)
    
    $someUser = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='username']")
    _WD_SetElementValue($sSession, $someUser, $aArrayTest2[$i])

    Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@type='submit'][@value='Submit']")
    _WD_ElementAction($sSession, $sElement, 'click')
    
    _WD_LoadWait($sSession)
    
    Sleep(5000)
    
    If $someUser Then
        Local $aArray2D[2] = ["test"]
        _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $aArray2D, "C2")
    EndIf
    
Next

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"]}}}}'
EndFunc   ;==>SetupChrome

 

test.html test.xlsx

Edited by goku200
Link to comment
Share on other sites

Thank you for the reply Bert. I modified my code to read the range and used the _ArrayDisplay to display my columns. It displays all 3 columns but still only writes to C2. Not sure why it's not continuing to the next cell (C3, C4,etc...

 

 

Edited by goku200
Link to comment
Share on other sites

Because you are writing a single string "test" to a single location.  It is done a number of times [ubound(...)-1] but it still writes to the same cell.  You need to modify the range or send an array instead of a single data.

Link to comment
Share on other sites

You did not understand what I was trying to explain.

First way (changing range) :

Local $aArrayTest3 = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.ActiveSheet.Usedrange.Columns("A:C"))
For $i = 0 To UBound($aArrayTest3) - 1
   _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $i, "C" & $i+2)
Next

Second way (passing an array) :

Local $aArrayTest3 = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.ActiveSheet.Usedrange.Columns("A:C"))
_Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $aArrayTest3, "C2")

 

Link to comment
Share on other sites

  • Developers

@goku200 or should I say @j1osu2002,

Why have you created a new account to ask similar questions?

And while I have your attention: What is the real purpose of this script?

@ everybody else, please stay out for the moment.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I had accidentally created an extra account and didn't realized i was using the other account. my purpose is to loop through the first loop and go to each link in the spreadsheeet and populate a number in the input field. If the input element is found I'm trying to write "test" in Column C starting at C2 and C3, etc...I got as far as to writing to the column but its writing test only to column C2. Apologize about the extra account

 

Edited by goku200
Link to comment
Share on other sites

  • Developers
1 hour ago, goku200 said:

I had accidentally created an extra account and didn't realized i was using the other account.

mmm accidently uh ... 🤔

I have merge the 2 accounts into your last one and please stick to the forum rules from here on. 
Thanks,
Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

×
×
  • Create New...