Laravel Dusk how to get multiple element's attributes?

蹲街弑〆低调 提交于 2019-12-08 01:57:21

问题


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');
}


来源:https://stackoverflow.com/questions/55150671/laravel-dusk-how-to-get-multiple-elements-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!