1

hi im using laravel dusk and i have inputs as array like this ..

<input type='text' name='debits[]' />
<input type='text' name='debits[]' />

how can i add value to the first or second or specific input using the key of the array like

public function create(Browser $browser)
{
    $browser
    ->select('journal_id',1)
    ->pause(100)
    ->click('.add-line')
    ->click('.add-line')
    // code here to add 10 to first debits
    // code here to add 15 to second debits

    ;
}

is that possible thanks a lot ..

2
  • 1
    stackoverflow.com/questions/44283669/… suggests using e.g. array_name[]:nth-child(1) Commented Apr 30, 2020 at 15:17
  • Can you push your code to GitHub? I mean the test part. Commented May 5, 2020 at 3:24

1 Answer 1

1
+50

You can do this:

    $names = collect([
    
        'name1',
        'name2'
    
    ]);
    $this->browse(function ($browser) use ($names) {
                $browser->visit('/names');
                $names->each(function ($name, $key) {
                  $browser->type("array[$key]", $name);
                });
                $browser->press('Send');
            });

Based on your code You can do this:

public function create(Browser $browser)
{
    $browser
    ->select('journal_id',1)
    ->pause(100)
    ->click('.add-line')
    ->click('.add-line');
    
    $inputs = $browser->elements('input[name^="debits["]');
    $inputs[0]->sendKeys(10); // code here to add 10 to first debits
    $inputs[1]->sendKeys(15); // code here to add 15 to second debits
}

If you want to do it with a for loop you can do this:

$inputs = $browser->elements('input[name^="debits["]');
foreach ($inputs as $input) {
  $input->sendKeys('100');
}
Sign up to request clarification or add additional context in comments.

3 Comments

i gat this error no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='debits[0]']"}
but if i change the code like this ->type("debits[]", 1) its working for the first input only
I've updated my answer again so let me know if you have any question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.