Angular JS, Protractor locator, get direct childre

2019-05-10 20:42发布

I have a grid, from which I want to select all the rows and none of the elements inside the rows

The rows are just divs, no classes etc etc, something like this (inner content removed for brevity)

<div class="grid">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

Normally with a protractor locator you could just go element(by.css(".grid > div"))

The issue I have is that grid is already a protractor element and I don't have control over it but I still want to only select its direct children.

var gridElement = element(by.css(".grid"));

So I'd need something like

var rows = gridElement.all(by.css("> div"));

But this is not a valid CSS selector as it's missing the left hand side. Does anyone know how I can achieve this?

EDIT: Using protractor 1.0, and updating the version is unfortunately a last-resort

2条回答
地球回转人心会变
2楼-- · 2019-05-10 21:05

As Cayce said, this should work:

var rows = gridElement.all(by.css("div:first-child"));

查看更多
ら.Afraid
3楼-- · 2019-05-10 21:14

You can also solve it with by.xpath():

var rows = gridElement.all(by.xpath("./div"));
查看更多
登录 后发表回答