Laravel Dusk如何获取多个元素的属性? [英] Laravel Dusk how to get multiple element's attributes?

查看:56
本文介绍了Laravel Dusk如何获取多个元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在Laravel 5.8上使用Laravel Dusk,已经遇到了问题.在Google上进行了大量搜索,但没有找到答案.

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.

我想做的就是获取所有链接,并获取它们的href属性或它们可能具有的任何自定义属性(我想在该属性中指定名称).

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).

我在文档中找到了这个

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

但是它并没有真正帮助我,因为它仅获得第一个(或最后一个,我不确定100%知道)元素的属性.

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?

推荐答案

使用getAttribute():

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

这篇关于Laravel Dusk如何获取多个元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆