javascript - Angular JS, Protractor locator, get direct children of element - Stack Overflow

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

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

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

Share Improve this question edited Apr 3, 2015 at 12:55 Sam asked Apr 3, 2015 at 8:32 SamSam 2,8212 gold badges29 silver badges42 bronze badges 4
  • have you tried div without > ??? – Cayce K Commented Apr 3, 2015 at 12:17
  • Yes, that would select all child divs which is not what I am going for, I just want direct descendants – Sam Commented Apr 3, 2015 at 12:52
  • I am not well versed in angular so I need to learn some any way, but I'm not 100% sure why you couldn't do something like $(this).children() where this is the selector of the element you are targeting. This is assuming you are doing some sort of onClick or onHover or something along those lines. (I would be under the assumption you could then do your angular attached to that element as it would be a valid jQuery element selected) – Cayce K Commented Apr 3, 2015 at 13:09
  • I did try various .children() methods and they didn't seem to be available – Sam Commented Apr 3, 2015 at 14:49
Add a ment  | 

2 Answers 2

Reset to default 9

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

var rows = gridElement.all(by.xpath("./div"));

As Cayce said, this should work:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342777a4623404.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信