简体   繁体   English

Laravel Dusk 如何在元素内部查找元素?

[英]Laravel Dusk how to find elements inside elements?

I'm using Laravel 5.8, Dusk.我正在使用 Laravel 5.8,黄昏。 I would like to find certain elements inside a class.我想在类中找到某些元素。

So let's say I extracted every element on the page with the class of selectable .因此,假设我使用selectable类提取了页面上的每个元素。

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

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

After this I iterate through these elements like this:在此之后,我像这样遍历这些元素:

foreach ($elems as $elem) {

}

The question is, how can I find every element with the class of .custom-item inside these .selectable classes.问题是,我如何才能在这些.selectable类中找到具有.custom-item类的每个元素。 Additionally, I'd like to get one of .custom-item 's attribute.此外,我想获得.custom-item的属性之一。 I used to get it like this:我曾经是这样得到的:

$elem->getAttribute('custom-attribute');

(Learned from this Laravel Dusk how to get multiple element's attributes? post) (从这个Laravel Dusk中学到了如何获取多个元素的属性?帖子)

So how can I find/extract elements inside elements and then get their custom attributes with Laravel Dusk?那么如何在元素中查找/提取元素,然后使用 Laravel Dusk 获取它们的自定义属性?

Laravel Dusk does not provide an API method to locate elements. Laravel Dusk 没有提供 API 方法来定位元素。 You can do it by utilising the underlying webdriver API.您可以通过使用底层的 webdriver API 来实现。

You can access the webdriver via您可以通过以下方式访问网络驱动程序

$browser->driver

And to find an element wihtin another element.并在另一个元素中找到一个元素。 You will probably need to approach it via xPath您可能需要通过 xPath 来处理它

$browser->driver->findElements(WebDriverBy::xpath('//*[@class="selectable"][@class="custom-attribute"]'));

I made up xpath in above example.我在上面的例子中编写了 xpath。 You can find the exact xpath by inspecting the element in chrome developer tool.您可以通过检查 chrome 开发人员工具中的元素来找到确切的 xpath。

You can read more about selectors in Dusk here您可以在此处阅读有关 Dusk 中选择器的更多信息

https://www.5balloons.info/understanding-selectors-laravel-dusk-browser-testing/ https://www.5balloons.info/understanding-selectors-laravel-dusk-browser-testing/

The easier way:更简单的方法:

$advs = $browser->elements('CSS_SELECTOR');

foreach ($advs as $adv) {

   // for example
   $adv->click();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM