Laravel Dusk how to get multiple element's att

2019-08-19 19:18发布

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条回答
We Are One
2楼-- · 2019-08-19 19:54

Use getAttribute():

foreach ($elems as $elem) {
    $elem->getAttribute('href');
}
查看更多
登录 后发表回答