Laravel Dusk how to get multiple element's att

2019-08-19 20:02发布

问题:

I just began using Laravel Dusk on Laravel 5.8 and already faced an issue. Was searching a lot on Google, but haven't found an answer.

$browser->visit('https://www.website.com')
        ->script('window.scrollTo(0, 1000);');

$elems = $browser              
          ->pause(1000)
          ->elements('.elem a');

This is my current code to get all the links under a certain element on the page.

What I want to do is get all the links and get their href attribute or any custom attribute they might have (I want to specify the name if the attribute).

I have found this in the documentation:

$attribute = $browser->attribute('selector', 'value');

But it doesn't really help me because it only gets the first (or last, I'm not 100% sure) element's attribute.

So is there any way to iterate through the found elements and extract their attributes?

回答1:

Use getAttribute():

foreach ($elems as $elem) {
    $elem->getAttribute('href');
}